Utility.cs 5.3 KB

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