Utility.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.IO;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. using System.Windows.Media.Imaging;
  7. namespace Phihong_EVSE_UI_Tool
  8. {
  9. public static class Utility
  10. {
  11. public const string BG_PARENTFOLDER = @"Background\";
  12. public const string BG_INITIAL = "0_init.bmp";
  13. public const string BG_IDLE = "1_idle.bmp";
  14. public const string BG_VERIFY = "2_verify.bmp";
  15. public const string BG_VERIFYOK = "3_carChkok.bmp";
  16. public const string BG_VERIFYFAIL = "4_chkfail.bmp";
  17. public const string BG_PLUG = "5_plug.bmp";
  18. public const string BG_PRECHARGE = "6_precharge.bmp";
  19. public const string BG_CHARGING = "7_charging.bmp";
  20. public const string BG_COMPLETE = "8_complete.bmp";
  21. public const string BG_MAINTAIN = "9_fix.bmp";
  22. public const string ICO_PARENTFOLDER = @"ICO\";
  23. public const string ICO_LOGO = "10_logo.bmp";
  24. public const string ICO_NAME = "60.ICO";
  25. public static readonly string OUTPUT_DIRECTORY = Environment.CurrentDirectory + @"\Output\";
  26. public static readonly string CUSTOM_DIRECTORY = Environment.CurrentDirectory + @"\CustomTemp\";
  27. public static void CopyFileFromResource(Uri uriString, string destPath)
  28. {
  29. using (var resource = Application.GetResourceStream(uriString).Stream)
  30. {
  31. using (var file = new FileStream(destPath, FileMode.Create, FileAccess.Write))
  32. {
  33. resource.CopyTo(file);
  34. }
  35. }
  36. }
  37. public static void LoadImageFromPath(Image imgControl, string path)
  38. {
  39. FileStream fstream = new FileStream(path, FileMode.Open);
  40. BitmapImage bitmap = new BitmapImage();
  41. bitmap.BeginInit();
  42. bitmap.StreamSource = fstream;
  43. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  44. bitmap.EndInit();
  45. fstream.Close();
  46. imgControl.BeginInit();
  47. imgControl.Source = bitmap;
  48. imgControl.EndInit();
  49. }
  50. public static bool IsBackgroundImageSizeQualified(string path)
  51. {
  52. FileStream fstream = new FileStream(path, FileMode.Open);
  53. BitmapImage bitmap = new BitmapImage();
  54. bitmap.BeginInit();
  55. bitmap.StreamSource = fstream;
  56. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  57. bitmap.EndInit();
  58. fstream.Close();
  59. if (bitmap.PixelWidth != 800 || bitmap.PixelHeight != 480)
  60. {
  61. return false;
  62. }
  63. return true;
  64. }
  65. public static bool IsIcoImageSizeQualified(string path)
  66. {
  67. FileStream fstream = new FileStream(path, FileMode.Open);
  68. BitmapImage bitmap = new BitmapImage();
  69. bitmap.BeginInit();
  70. bitmap.StreamSource = fstream;
  71. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  72. bitmap.EndInit();
  73. fstream.Close();
  74. if (bitmap.PixelWidth > 255 || bitmap.PixelHeight > 255)
  75. {
  76. return false;
  77. }
  78. return true;
  79. }
  80. public static bool IsIcoLogoSizeQualified(string path)
  81. {
  82. FileStream fstream = new FileStream(path, FileMode.Open);
  83. BitmapImage bitmap = new BitmapImage();
  84. bitmap.BeginInit();
  85. bitmap.StreamSource = fstream;
  86. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  87. bitmap.EndInit();
  88. fstream.Close();
  89. if (bitmap.PixelWidth != 186 || bitmap.PixelHeight != 34)
  90. {
  91. return false;
  92. }
  93. return true;
  94. }
  95. public static void ConvertToBitmap565Array(string path, out int width, out int height, out byte[] pixels)
  96. {
  97. FileStream fstream = new FileStream(path, FileMode.Open);
  98. BitmapImage bitmap = new BitmapImage();
  99. bitmap.BeginInit();
  100. bitmap.StreamSource = fstream;
  101. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  102. bitmap.EndInit();
  103. fstream.Close();
  104. FormatConvertedBitmap newFormated = new FormatConvertedBitmap();
  105. newFormated.BeginInit();
  106. newFormated.Source = bitmap;
  107. newFormated.DestinationFormat = PixelFormats.Bgr565;
  108. newFormated.EndInit();
  109. width = newFormated.PixelWidth;
  110. height = newFormated.PixelHeight;
  111. var stride = (newFormated.PixelWidth * newFormated.Format.BitsPerPixel + 7) / 8;
  112. pixels = new byte[newFormated.PixelHeight * stride];
  113. newFormated.CopyPixels(pixels, stride, 0);
  114. for (int i = 0; i < pixels.Length; i += 2)
  115. {
  116. byte temp = pixels[i];
  117. pixels[i] = pixels[i + 1];
  118. pixels[i + 1] = temp;
  119. }
  120. }
  121. }
  122. }