ModifyIco.xaml.cs 11 KB

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