ModifyIco.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 string urlCustomImgPath;
  19. private int bytesPerPixel = 2;
  20. public ModifyIco()
  21. {
  22. InitializeComponent();
  23. logoCustomImgPath = mySettings.LogoCustomImgPath;
  24. uxLogoDefaultRadioButton.IsChecked = mySettings.IsLogoDefault && !mySettings.IsLogoHide;
  25. uxLogoCustomRadioButton.IsChecked = !mySettings.IsLogoDefault && !mySettings.IsLogoHide;
  26. uxLogoHideRadioButton.IsChecked = mySettings.IsLogoHide;
  27. uxLogoBrowseTextBox.Text = mySettings.LogoCustomImgPath_org;
  28. compLogoCustomImgPath = mySettings.CompLogoCustomImgPath;
  29. uxCompLogoDefaultRadioButton.IsChecked = mySettings.IsCompLogoDefault && !mySettings.IsCompLogoHide;
  30. uxCompLogoCustomRadioButton.IsChecked = !mySettings.IsCompLogoDefault && !mySettings.IsCompLogoHide;
  31. uxCompLogoHideRadioButton.IsChecked = mySettings.IsCompLogoHide;
  32. uxCompLogoBrowseTextBox.Text = mySettings.CompLogoCustomImgPath_org;
  33. urlCustomImgPath = mySettings.UrlCustomImgPath;
  34. uxUrlDefaultRadioButton.IsChecked = mySettings.IsUrlDefault && !mySettings.IsUrlHide;
  35. uxUrlCustomRadioButton.IsChecked = !mySettings.IsUrlDefault && !mySettings.IsUrlHide;
  36. uxUrlHideRadioButton.IsChecked = mySettings.IsUrlHide;
  37. uxUrlBrowseTextBox.Text = mySettings.UrlCustomImgPath_org;
  38. }
  39. private void ModifyIcoContent(int index, string path, bool isHide = false)
  40. {
  41. string icoPath = Path.Combine(Utility.OUTPUT_DIRECTORY, Utility.ICO_NAME);
  42. if (!File.Exists(icoPath))
  43. {
  44. Uri srcPath = new Uri(Path.Combine(Utility.ICO_PARENTFOLDER, Utility.ICO_NAME), UriKind.Relative);
  45. Utility.CopyFileFromResource(srcPath, icoPath);
  46. }
  47. int width = 0, height = 0;
  48. byte[] pixels = null;
  49. if (String.IsNullOrEmpty(path) && !isHide)
  50. {
  51. //Default
  52. Uri resourcePath = new Uri(Path.Combine(Utility.ICO_PARENTFOLDER, index.ToString() + ".bmp"), UriKind.Relative);
  53. Utility.ConvertResourceToBitmap565Array(resourcePath, out width, out height, out pixels);
  54. }
  55. else if (String.IsNullOrEmpty(path) && isHide)
  56. {
  57. //Hide
  58. Uri resourcePath = new Uri(Path.Combine(Utility.ICO_PARENTFOLDER, index.ToString() + "_hide.bmp"), UriKind.Relative);
  59. Utility.ConvertResourceToBitmap565Array(resourcePath, out width, out height, out pixels);
  60. }
  61. else
  62. {
  63. Utility.ConvertFileToBitmap565Array(path, out width, out height, out pixels);
  64. }
  65. FileStream icoFile = new FileStream(Path.Combine(Utility.OUTPUT_DIRECTORY, Utility.ICO_NAME),
  66. FileMode.OpenOrCreate, FileAccess.ReadWrite);
  67. byte[] array = new byte[4 * bytesPerPixel];
  68. icoFile.Position = index * 4 * bytesPerPixel;
  69. icoFile.Read(array, 0, 4 * bytesPerPixel);
  70. long offset = ((array[2] << 24) + (array[3] << 16) + (array[4] << 8) + array[5]) * bytesPerPixel;
  71. array[6] = pixels[0];
  72. array[7] = pixels[1];
  73. //Modify Header
  74. icoFile.Position = index * 4 * bytesPerPixel; //header offset
  75. icoFile.Write(array, 0, 4 * bytesPerPixel);
  76. //Modify Content
  77. icoFile.Position = offset;
  78. icoFile.Write(pixels, 0, width * height * bytesPerPixel);
  79. icoFile.Close();
  80. }
  81. private void DisplayDWIcoContent()
  82. {
  83. FileStream fs = new FileStream(Path.Combine(Utility.OUTPUT_DIRECTORY, Utility.ICO_NAME),
  84. FileMode.OpenOrCreate, FileAccess.ReadWrite);
  85. if (fs.Length > 131072 * bytesPerPixel)
  86. {
  87. for (int i = 0; i <= 32767; i++)
  88. {
  89. byte[] array = new byte[4 * bytesPerPixel];
  90. fs.Position = i * 4 * bytesPerPixel;
  91. fs.Read(array, 0, 4 * bytesPerPixel);
  92. int width = array[0];
  93. int height = array[1];
  94. long offset = ((array[2] << 24) + (array[3] << 16) + (array[4] << 8) + array[5]) * bytesPerPixel;
  95. if (offset <= 0)
  96. {
  97. continue;
  98. }
  99. if (offset >= 131072 * bytesPerPixel)
  100. {
  101. DisPlayIco(fs, width, height, offset);
  102. Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() => { }));
  103. }
  104. }
  105. }
  106. fs.Close();
  107. }
  108. public void DisPlayIco(FileStream fs, int width, int height, long offset)
  109. {
  110. WriteableBitmap wbitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgr565, null);
  111. byte[,,] pixels = new byte[width, height, 2];
  112. byte[] pixels1d = new byte[height * width * bytesPerPixel];
  113. fs.Position = offset;
  114. byte[] array = new byte[bytesPerPixel];
  115. for (int i = 0; i < height; i++)
  116. {
  117. for (int j = 0; j < width; j++)
  118. {
  119. fs.Read(array, 0, bytesPerPixel);
  120. pixels[j, i, 0] = array[1];
  121. pixels[j, i, 1] = array[0];
  122. }
  123. }
  124. int index = 0;
  125. for (int row = 0; row < height; row++)
  126. {
  127. for (int col = 0; col < width; col++)
  128. {
  129. for (int i = 0; i < bytesPerPixel; i++)
  130. {
  131. pixels1d[index++] = pixels[col, row, i];
  132. }
  133. }
  134. }
  135. Int32Rect rect = new Int32Rect(0, 0, width, height);
  136. int stride = (width * wbitmap.Format.BitsPerPixel + 7) / 8;
  137. wbitmap.WritePixels(rect, pixels1d, stride, 0);
  138. Image image = new Image()
  139. {
  140. Width = 270,
  141. Height = 270,
  142. Stretch = Stretch.None,
  143. Margin = new Thickness(0),
  144. Source = wbitmap
  145. };
  146. RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality);
  147. uxIcoUniformGrid.Children.Add(image);
  148. }
  149. private void uxDefaultRadioButton_Checked(object sender, RoutedEventArgs e)
  150. {
  151. Uri srcPath;
  152. RadioButton rb = e.Source as RadioButton;
  153. if (rb is null)
  154. {
  155. return;
  156. }
  157. switch (rb.Tag.ToString())
  158. {
  159. case "Logo":
  160. srcPath = new Uri(Path.Combine(Utility.ICO_PARENTFOLDER, Utility.ICO_MAINLOGO), UriKind.Relative);
  161. uxLogoImage.Source = new BitmapImage(srcPath);
  162. mySettings.IsLogoDefault = true;
  163. mySettings.IsLogoHide = false;
  164. ModifyIcoContent((int)IcoElement.Logo, String.Empty);
  165. break;
  166. case "CompLogo":
  167. srcPath = new Uri(Path.Combine(Utility.ICO_PARENTFOLDER, Utility.ICO_COMPLETELOGO), UriKind.Relative);
  168. uxCompLogoImage.Source = new BitmapImage(srcPath);
  169. mySettings.IsCompLogoDefault = true;
  170. mySettings.IsCompLogoHide = false;
  171. ModifyIcoContent((int)IcoElement.LogoComplete, String.Empty);
  172. break;
  173. case "Url":
  174. srcPath = new Uri(Path.Combine(Utility.ICO_PARENTFOLDER, Utility.ICO_URL), UriKind.Relative);
  175. uxUrlImage.Source = new BitmapImage(srcPath);
  176. mySettings.IsUrlDefault = true;
  177. mySettings.IsUrlHide = false;
  178. ModifyIcoContent((int)IcoElement.URL, String.Empty);
  179. break;
  180. default:
  181. return;
  182. }
  183. Utility.ChangedFiles[Utility.ICO_NAME] = true;
  184. mySettings.Save();
  185. //Display Ico
  186. uxIcoUniformGrid.Children.Clear();
  187. DisplayDWIcoContent();
  188. }
  189. private void uxCustomRadioButton_Checked(object sender, RoutedEventArgs e)
  190. {
  191. RadioButton rb = e.Source as RadioButton;
  192. if (rb is null)
  193. {
  194. return;
  195. }
  196. switch (rb.Tag.ToString())
  197. {
  198. case "Logo":
  199. if (File.Exists(logoCustomImgPath))
  200. {
  201. Utility.LoadImageFromPath(uxLogoImage, logoCustomImgPath);
  202. mySettings.IsLogoDefault = false;
  203. mySettings.IsLogoHide = false;
  204. ModifyIcoContent((int)IcoElement.Logo, logoCustomImgPath);
  205. }
  206. break;
  207. case "CompLogo":
  208. if (File.Exists(compLogoCustomImgPath))
  209. {
  210. Utility.LoadImageFromPath(uxCompLogoImage, compLogoCustomImgPath);
  211. mySettings.IsCompLogoDefault = false;
  212. mySettings.IsCompLogoHide = false;
  213. ModifyIcoContent((int)IcoElement.LogoComplete, compLogoCustomImgPath);
  214. }
  215. break;
  216. case "Url":
  217. if (File.Exists(urlCustomImgPath))
  218. {
  219. Utility.LoadImageFromPath(uxUrlImage, urlCustomImgPath);
  220. mySettings.IsUrlDefault = false;
  221. mySettings.IsUrlHide = false;
  222. ModifyIcoContent((int)IcoElement.URL, urlCustomImgPath);
  223. }
  224. break;
  225. default:
  226. return;
  227. }
  228. Utility.ChangedFiles[Utility.ICO_NAME] = true;
  229. mySettings.Save();
  230. //Display Ico
  231. uxIcoUniformGrid.Children.Clear();
  232. DisplayDWIcoContent();
  233. }
  234. private void uxHideRadioButton_Checked(object sender, RoutedEventArgs e)
  235. {
  236. Uri srcPath;
  237. RadioButton rb = e.Source as RadioButton;
  238. if (rb is null)
  239. {
  240. return;
  241. }
  242. switch (rb.Tag.ToString())
  243. {
  244. case "Logo":
  245. srcPath = new Uri(Path.Combine(Utility.ICO_PARENTFOLDER, Utility.ICO_MAINLOGO_HIDE), UriKind.Relative);
  246. uxLogoImage.Source = new BitmapImage(srcPath);
  247. mySettings.IsLogoDefault = false;
  248. mySettings.IsLogoHide = true;
  249. ModifyIcoContent((int)IcoElement.Logo, String.Empty, true);
  250. break;
  251. case "CompLogo":
  252. srcPath = new Uri(Path.Combine(Utility.ICO_PARENTFOLDER, Utility.ICO_COMPLETELOGO_HIDE), UriKind.Relative);
  253. uxCompLogoImage.Source = new BitmapImage(srcPath);
  254. mySettings.IsCompLogoDefault = false;
  255. mySettings.IsCompLogoHide = true;
  256. ModifyIcoContent((int)IcoElement.LogoComplete, String.Empty, true);
  257. break;
  258. case "Url":
  259. srcPath = new Uri(Path.Combine(Utility.ICO_PARENTFOLDER, Utility.ICO_URL_HIDE), UriKind.Relative);
  260. uxUrlImage.Source = new BitmapImage(srcPath);
  261. mySettings.IsUrlDefault = false;
  262. mySettings.IsUrlHide = true;
  263. ModifyIcoContent((int)IcoElement.URL, String.Empty, true);
  264. break;
  265. default:
  266. return;
  267. }
  268. Utility.ChangedFiles[Utility.ICO_NAME] = true;
  269. mySettings.Save();
  270. //Display Ico
  271. uxIcoUniformGrid.Children.Clear();
  272. DisplayDWIcoContent();
  273. }
  274. private void uxBrowseButton_Click(object sender, RoutedEventArgs e)
  275. {
  276. Button btn = e.Source as Button;
  277. if (btn is null)
  278. {
  279. return;
  280. }
  281. var dlg = new CommonOpenFileDialog()
  282. {
  283. EnsureFileExists = true,
  284. Title = "Select an image file",
  285. };
  286. dlg.Filters.Add(new CommonFileDialogFilter("BMP images", "*.bmp"));
  287. if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
  288. {
  289. switch (btn.Tag.ToString())
  290. {
  291. case "Logo":
  292. if (!Utility.IsIcoLogoSizeQualified(dlg.FileName))
  293. {
  294. MessageBox.Show(dlg.FileName + "\r\n" + "The resolution of the image must be 255x34",
  295. "Incorrect Resolution", MessageBoxButton.OK, MessageBoxImage.Error);
  296. return;
  297. }
  298. uxLogoBrowseTextBox.Text = dlg.FileName;
  299. logoCustomImgPath = Path.Combine(Utility.CUSTOM_DIRECTORY, Utility.ICO_MAINLOGO);
  300. File.Copy(dlg.FileName, logoCustomImgPath, true);
  301. Utility.LoadImageFromPath(uxLogoImage, logoCustomImgPath);
  302. mySettings.IsLogoDefault = false;
  303. mySettings.LogoCustomImgPath = logoCustomImgPath;
  304. mySettings.LogoCustomImgPath_org = uxLogoBrowseTextBox.Text;
  305. ModifyIcoContent((int)IcoElement.Logo, logoCustomImgPath);
  306. break;
  307. case "CompLogo":
  308. if (!Utility.IsIcoLogoSizeQualified(dlg.FileName))
  309. {
  310. MessageBox.Show(dlg.FileName + "\r\n" + "The resolution of the image must be 255x34",
  311. "Incorrect Resolution", MessageBoxButton.OK, MessageBoxImage.Error);
  312. return;
  313. }
  314. uxCompLogoBrowseTextBox.Text = dlg.FileName;
  315. compLogoCustomImgPath = Path.Combine(Utility.CUSTOM_DIRECTORY, Utility.ICO_COMPLETELOGO);
  316. File.Copy(dlg.FileName, compLogoCustomImgPath, true);
  317. Utility.LoadImageFromPath(uxCompLogoImage, compLogoCustomImgPath);
  318. mySettings.IsCompLogoDefault = false;
  319. mySettings.CompLogoCustomImgPath = compLogoCustomImgPath;
  320. mySettings.CompLogoCustomImgPath_org = uxCompLogoBrowseTextBox.Text;
  321. ModifyIcoContent((int)IcoElement.LogoComplete, compLogoCustomImgPath);
  322. break;
  323. case "Url":
  324. if (!Utility.IsIcoUrlSizeQualified(dlg.FileName))
  325. {
  326. MessageBox.Show(dlg.FileName + "\r\n" + "The resolution of the image must be 233x28",
  327. "Incorrect Resolution", MessageBoxButton.OK, MessageBoxImage.Error);
  328. return;
  329. }
  330. uxUrlBrowseTextBox.Text = dlg.FileName;
  331. urlCustomImgPath = Path.Combine(Utility.CUSTOM_DIRECTORY, Utility.ICO_URL);
  332. File.Copy(dlg.FileName, urlCustomImgPath, true);
  333. Utility.LoadImageFromPath(uxUrlImage, urlCustomImgPath);
  334. mySettings.IsUrlDefault = false;
  335. mySettings.UrlCustomImgPath = urlCustomImgPath;
  336. mySettings.UrlCustomImgPath_org = uxUrlBrowseTextBox.Text;
  337. ModifyIcoContent((int)IcoElement.URL, urlCustomImgPath);
  338. break;
  339. default:
  340. break;
  341. }
  342. Utility.ChangedFiles[Utility.ICO_NAME] = true;
  343. mySettings.Save();
  344. //Display Ico
  345. uxIcoUniformGrid.Children.Clear();
  346. DisplayDWIcoContent();
  347. }
  348. }
  349. }
  350. }