BuildIco.xaml.cs 7.7 KB

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