ModifyIco.xaml.cs 8.8 KB

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