BuildIco.xaml.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using Microsoft.WindowsAPICodePack.Dialogs;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace Phihong_EVSE_UI_Tool
  19. {
  20. /// <summary>
  21. /// BuildIco.xaml 的互動邏輯
  22. /// </summary>
  23. public partial class BuildIco : UserControl
  24. {
  25. private string imgFolderPath;
  26. private string icoFileName;
  27. private List<ImageList> imageList;
  28. private int bytesPerPixel = 2;
  29. public BuildIco()
  30. {
  31. InitializeComponent();
  32. imageList = new List<ImageList>();
  33. }
  34. private void GenerateImageList()
  35. {
  36. imageList.Clear();
  37. uxDataStackPanel.Visibility = Visibility.Visible;
  38. uxBuildButton.Visibility = Visibility.Collapsed;
  39. uxIcoUniformGrid.Visibility = Visibility.Collapsed;
  40. uxImagesDataGrid.ItemsSource = null;
  41. uxImagesDataGrid.Items.Refresh();
  42. Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() => { }));
  43. DirectoryInfo directoryInfo = new DirectoryInfo(imgFolderPath);
  44. var files = directoryInfo.GetFiles().Where(s => s.Extension.ToLower() == ".bmp" ||
  45. s.Extension.ToLower() == ".jpg" ||
  46. s.Extension.ToLower() == ".png");
  47. foreach (FileInfo f in files)
  48. {
  49. int idx = f.Name.IndexOf(f.Name.FirstOrDefault<char>(e => !char.IsNumber(e)));
  50. if (idx != -1)
  51. {
  52. if (!Utility.IsIcoImageSizeQualified(f.FullName))
  53. {
  54. MessageBox.Show("Warning! " + f.Name + "\r\n" +
  55. "The width and height of the image must be <= 255");
  56. continue;
  57. }
  58. int index = Int32.Parse(f.Name.Substring(0, idx));
  59. int width = 0, height = 0;
  60. byte[] pixels = null;
  61. Utility.ConvertToBitmap565Array(f.FullName, out width, out height, out pixels);
  62. imageList.Add(new ImageList(index, System.IO.Path.GetFileName(f.FullName),
  63. (byte)width, (byte)height, pixels));
  64. }
  65. }
  66. imageList = imageList.OrderBy(e => e.Index).ToList();
  67. uxImagesDataGrid.ItemsSource = imageList;
  68. uxBuildButton.Visibility = Visibility.Visible;
  69. uxIcoUniformGrid.Visibility = Visibility.Visible;
  70. }
  71. private void BuildIcoFile()
  72. {
  73. icoFileName = Utility.OUTPUT_DIRECTORY + Utility.ICO_NAME;
  74. FileStream icoFile = new FileStream(icoFileName, FileMode.Create, FileAccess.ReadWrite);
  75. byte[] array = new byte[4 * bytesPerPixel];
  76. long contentOffset = 131072 * bytesPerPixel;
  77. foreach (ImageList i in imageList)
  78. {
  79. array[0] = i.PixelWidth;
  80. array[1] = i.PixelHeight;
  81. array[2] = (byte)(contentOffset / bytesPerPixel >> 24);
  82. array[3] = (byte)(contentOffset / bytesPerPixel >> 16);
  83. array[4] = (byte)(contentOffset / bytesPerPixel >> 8);
  84. array[5] = (byte)(contentOffset / bytesPerPixel);
  85. array[6] = i.Pixels[0];
  86. array[7] = i.Pixels[1];
  87. //Header
  88. icoFile.Position = i.Index * 4 * bytesPerPixel; //header offset
  89. icoFile.Write(array, 0, 4 * bytesPerPixel);
  90. //Content
  91. icoFile.Position = contentOffset;
  92. icoFile.Write(i.Pixels, 0, i.PixelWidth * i.PixelHeight * bytesPerPixel);
  93. contentOffset += i.PixelWidth * i.PixelHeight * bytesPerPixel;
  94. }
  95. icoFile.Close();
  96. }
  97. private void DisplayDWIcoContent()
  98. {
  99. FileStream fs = new FileStream(icoFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  100. if (fs.Length > 131072 * bytesPerPixel)
  101. {
  102. for (int i = 0; i <= 32767; i++)
  103. {
  104. byte[] array = new byte[4 * bytesPerPixel];
  105. fs.Position = i * 4 * bytesPerPixel;
  106. fs.Read(array, 0, 4 * bytesPerPixel);
  107. int width = array[0];
  108. int height = array[1];
  109. long offset = ((array[2] << 24) + (array[3] << 16) + (array[4] << 8) + array[5]) * bytesPerPixel;
  110. if (offset <= 0)
  111. {
  112. continue;
  113. }
  114. if (offset >= 131072 * bytesPerPixel)
  115. {
  116. DisPlayIco(fs, width, height, offset);
  117. Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() => { }));
  118. }
  119. }
  120. }
  121. fs.Close();
  122. }
  123. public void DisPlayIco(FileStream fs, int width, int height, long offset)
  124. {
  125. WriteableBitmap wbitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgr565, null);
  126. byte[,,] pixels = new byte[width, height, 2];
  127. byte[] pixels1d = new byte[height * width * bytesPerPixel];
  128. fs.Position = offset;
  129. byte[] array = new byte[bytesPerPixel];
  130. for (int i = 0; i < height; i++)
  131. {
  132. for (int j = 0; j < width; j++)
  133. {
  134. fs.Read(array, 0, bytesPerPixel);
  135. pixels[j, i, 0] = array[1];
  136. pixels[j, i, 1] = array[0];
  137. }
  138. }
  139. int index = 0;
  140. for (int row = 0; row < height; row++)
  141. {
  142. for (int col = 0; col < width; col++)
  143. {
  144. for (int i = 0; i < bytesPerPixel; i++)
  145. {
  146. pixels1d[index++] = pixels[col, row, i];
  147. }
  148. }
  149. }
  150. Int32Rect rect = new Int32Rect(0, 0, width, height);
  151. int stride = (width * wbitmap.Format.BitsPerPixel + 7) / 8;
  152. wbitmap.WritePixels(rect, pixels1d, stride, 0);
  153. Image image = new Image()
  154. {
  155. Width = 200,
  156. Height = 200,
  157. Stretch = Stretch.None,
  158. Margin = new Thickness(0),
  159. Source = wbitmap
  160. };
  161. RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality);
  162. uxIcoUniformGrid.Children.Add(image);
  163. }
  164. private void uxBrowseButton_Click(object sender, RoutedEventArgs e)
  165. {
  166. var dlg = new CommonOpenFileDialog()
  167. {
  168. IsFolderPicker = true,
  169. Title = "Select image files path",
  170. };
  171. if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
  172. {
  173. imgFolderPath = dlg.FileName;
  174. uxImageBrowseTextBox.Text = imgFolderPath;
  175. GenerateImageList();
  176. }
  177. }
  178. private void uxRefreshButton_Click(object sender, RoutedEventArgs e)
  179. {
  180. if (Directory.Exists(imgFolderPath))
  181. {
  182. GenerateImageList();
  183. }
  184. }
  185. private void uxBuildButton_Click(object sender, RoutedEventArgs e)
  186. {
  187. uxIcoUniformGrid.Children.Clear();
  188. BuildIcoFile();
  189. DisplayDWIcoContent();
  190. }
  191. }
  192. }