ModifyIco.xaml.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using Microsoft.WindowsAPICodePack.Dialogs;
  2. using System;
  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. /// <summary>
  11. /// ModifyIco.xaml 的互動邏輯
  12. /// </summary>
  13. public partial class ModifyIco : UserControl
  14. {
  15. private Properties.Settings mySettings = Properties.Settings.Default;
  16. private string logoCustomImgPath;
  17. private int bytesPerPixel = 2;
  18. public ModifyIco()
  19. {
  20. InitializeComponent();
  21. logoCustomImgPath = mySettings.LogoCustomImgPath;
  22. uxLogoDefaultRadioButton.IsChecked = mySettings.IsLogoDefault;
  23. uxLogoCustomRadioButton.IsChecked = !mySettings.IsLogoDefault;
  24. uxLogoBrowseTextBox.Text = mySettings.LogoCustomImgPath_org;
  25. }
  26. private void ModifyIcoContent(int index)
  27. {
  28. Uri srcPath = new Uri(Utility.ICO_PARENTFOLDER + Utility.ICO_NAME, UriKind.Relative);
  29. Utility.CopyFileFromResource(srcPath, Utility.OUTPUT_DIRECTORY + Utility.ICO_NAME);
  30. if (mySettings.IsLogoDefault)
  31. {
  32. return;
  33. }
  34. int width = 0, height = 0;
  35. byte[] pixels = null;
  36. Utility.ConvertToBitmap565Array(logoCustomImgPath, out width, out height, out pixels);
  37. FileStream icoFile = new FileStream(Utility.OUTPUT_DIRECTORY + Utility.ICO_NAME, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  38. byte[] array = new byte[4 * bytesPerPixel];
  39. icoFile.Position = index * 4 * bytesPerPixel;
  40. icoFile.Read(array, 0, 4 * bytesPerPixel);
  41. long offset = ((array[2] << 24) + (array[3] << 16) + (array[4] << 8) + array[5]) * bytesPerPixel;
  42. array[6] = pixels[0];
  43. array[7] = pixels[1];
  44. //Modify Header
  45. icoFile.Position = index * 4 * bytesPerPixel; //header offset
  46. icoFile.Write(array, 0, 4 * bytesPerPixel);
  47. //Modify Content
  48. icoFile.Position = offset;
  49. icoFile.Write(pixels, 0, width * height * bytesPerPixel);
  50. icoFile.Close();
  51. }
  52. private void DisplayDWIcoContent()
  53. {
  54. FileStream fs = new FileStream(Utility.OUTPUT_DIRECTORY + Utility.ICO_NAME, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  55. if (fs.Length > 131072 * bytesPerPixel)
  56. {
  57. for (int i = 0; i <= 32767; i++)
  58. {
  59. byte[] array = new byte[4 * bytesPerPixel];
  60. fs.Position = i * 4 * bytesPerPixel;
  61. fs.Read(array, 0, 4 * bytesPerPixel);
  62. int width = array[0];
  63. int height = array[1];
  64. long offset = ((array[2] << 24) + (array[3] << 16) + (array[4] << 8) + array[5]) * bytesPerPixel;
  65. if (offset <= 0)
  66. {
  67. continue;
  68. }
  69. if (offset >= 131072 * bytesPerPixel)
  70. {
  71. DisPlayIco(fs, width, height, offset);
  72. Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() => { }));
  73. }
  74. }
  75. }
  76. fs.Close();
  77. }
  78. public void DisPlayIco(FileStream fs, int width, int height, long offset)
  79. {
  80. WriteableBitmap wbitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgr565, null);
  81. byte[,,] pixels = new byte[width, height, 2];
  82. byte[] pixels1d = new byte[height * width * bytesPerPixel];
  83. fs.Position = offset;
  84. byte[] array = new byte[bytesPerPixel];
  85. for (int i = 0; i < height; i++)
  86. {
  87. for (int j = 0; j < width; j++)
  88. {
  89. fs.Read(array, 0, bytesPerPixel);
  90. pixels[j, i, 0] = array[1];
  91. pixels[j, i, 1] = array[0];
  92. }
  93. }
  94. int index = 0;
  95. for (int row = 0; row < height; row++)
  96. {
  97. for (int col = 0; col < width; col++)
  98. {
  99. for (int i = 0; i < bytesPerPixel; i++)
  100. {
  101. pixels1d[index++] = pixels[col, row, i];
  102. }
  103. }
  104. }
  105. Int32Rect rect = new Int32Rect(0, 0, width, height);
  106. int stride = (width * wbitmap.Format.BitsPerPixel + 7) / 8;
  107. wbitmap.WritePixels(rect, pixels1d, stride, 0);
  108. Image image = new Image()
  109. {
  110. Width = 200,
  111. Height = 200,
  112. Stretch = Stretch.None,
  113. Margin = new Thickness(0),
  114. Source = wbitmap
  115. };
  116. RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality);
  117. uxIcoUniformGrid.Children.Add(image);
  118. }
  119. private void uxDefaultRadioButton_Checked(object sender, RoutedEventArgs e)
  120. {
  121. Uri srcPath;
  122. RadioButton rb = e.Source as RadioButton;
  123. if (rb is null)
  124. {
  125. return;
  126. }
  127. switch (rb.Tag.ToString())
  128. {
  129. case "Logo":
  130. srcPath = new Uri(Utility.ICO_PARENTFOLDER + Utility.ICO_LOGO, UriKind.Relative);
  131. uxLogoImage.Source = new BitmapImage(srcPath);
  132. mySettings.IsLogoDefault = true;
  133. break;
  134. default:
  135. return;
  136. }
  137. mySettings.Save();
  138. ModifyIcoContent((int)IcoElement.Logo);
  139. //Display Ico
  140. uxIcoUniformGrid.Children.Clear();
  141. DisplayDWIcoContent();
  142. }
  143. private void uxCustomRadioButton_Checked(object sender, RoutedEventArgs e)
  144. {
  145. RadioButton rb = e.Source as RadioButton;
  146. if (rb is null)
  147. {
  148. return;
  149. }
  150. switch (rb.Tag.ToString())
  151. {
  152. case "Logo":
  153. if (File.Exists(logoCustomImgPath))
  154. {
  155. Utility.LoadImageFromPath(uxLogoImage, logoCustomImgPath);
  156. mySettings.IsLogoDefault = false;
  157. ModifyIcoContent((int)IcoElement.Logo);
  158. //Display Ico
  159. uxIcoUniformGrid.Children.Clear();
  160. DisplayDWIcoContent();
  161. }
  162. break;
  163. default:
  164. return;
  165. }
  166. mySettings.Save();
  167. }
  168. private void uxBrowseButton_Click(object sender, RoutedEventArgs e)
  169. {
  170. Button btn = e.Source as Button;
  171. if (btn is null)
  172. {
  173. return;
  174. }
  175. var dlg = new CommonOpenFileDialog()
  176. {
  177. EnsureFileExists = true,
  178. Title = "Select an image file",
  179. };
  180. dlg.Filters.Add(new CommonFileDialogFilter("BMP images", "*.bmp"));
  181. if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
  182. {
  183. if (!Utility.IsIcoLogoSizeQualified(dlg.FileName))
  184. {
  185. MessageBox.Show(dlg.FileName + "\r\n" + "The resolution of the image must be 186x34",
  186. "Incorrect Resolution", MessageBoxButton.OK, MessageBoxImage.Error);
  187. return;
  188. }
  189. switch (btn.Tag.ToString())
  190. {
  191. case "Logo":
  192. uxLogoBrowseTextBox.Text = dlg.FileName;
  193. logoCustomImgPath = Utility.CUSTOM_DIRECTORY + Utility.ICO_LOGO;
  194. File.Copy(dlg.FileName, logoCustomImgPath, true);
  195. Utility.LoadImageFromPath(uxLogoImage, logoCustomImgPath);
  196. mySettings.IsLogoDefault = false;
  197. mySettings.LogoCustomImgPath = logoCustomImgPath;
  198. mySettings.LogoCustomImgPath_org = uxLogoBrowseTextBox.Text;
  199. ModifyIcoContent((int)IcoElement.Logo);
  200. //Display Ico
  201. uxIcoUniformGrid.Children.Clear();
  202. DisplayDWIcoContent();
  203. break;
  204. default:
  205. break;
  206. }
  207. }
  208. mySettings.Save();
  209. }
  210. }
  211. }