123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- using Microsoft.WindowsAPICodePack.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- namespace Phihong_EVSE_UI_Tool
- {
- /// <summary>
- /// BuildIco.xaml 的互動邏輯
- /// </summary>
- public partial class BuildIco : UserControl
- {
- private string imgFolderPath;
- private string icoFileName;
- private List<ImageList> imageList;
- private int bytesPerPixel = 2;
- public BuildIco()
- {
- InitializeComponent();
- imageList = new List<ImageList>();
- }
- private void GenerateImageList()
- {
- imageList.Clear();
- uxDataStackPanel.Visibility = Visibility.Visible;
- uxBuildButton.Visibility = Visibility.Collapsed;
- uxIcoUniformGrid.Visibility = Visibility.Collapsed;
- uxImagesDataGrid.ItemsSource = null;
- uxImagesDataGrid.Items.Refresh();
- Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() => { }));
- DirectoryInfo directoryInfo = new DirectoryInfo(imgFolderPath);
- var files = directoryInfo.GetFiles().Where(s => s.Extension.ToLower() == ".bmp" ||
- s.Extension.ToLower() == ".jpg" ||
- s.Extension.ToLower() == ".png");
- foreach (FileInfo f in files)
- {
- int idx = f.Name.IndexOf(f.Name.FirstOrDefault<char>(e => !char.IsNumber(e)));
- if (idx != -1)
- {
- if (!Utility.IsIcoImageSizeQualified(f.FullName))
- {
- MessageBox.Show("Warning! " + f.Name + "\r\n" +
- "The width and height of the image must be <= 255");
- continue;
- }
- int index = Int32.Parse(f.Name.Substring(0, idx));
- int width = 0, height = 0;
- byte[] pixels = null;
- Utility.ConvertFileToBitmap565Array(f.FullName, out width, out height, out pixels);
- imageList.Add(new ImageList(index, System.IO.Path.GetFileName(f.FullName),
- (byte)width, (byte)height, pixels));
- }
- }
- imageList = imageList.OrderBy(e => e.Index).ToList();
- uxImagesDataGrid.ItemsSource = imageList;
- uxBuildButton.Visibility = Visibility.Visible;
- uxIcoUniformGrid.Visibility = Visibility.Visible;
- }
- private void BuildIcoFile()
- {
- icoFileName = Path.Combine(Utility.OUTPUT_DIRECTORY, Utility.ICO_NAME);
- FileStream icoFile = new FileStream(icoFileName, FileMode.Create, FileAccess.ReadWrite);
- byte[] array = new byte[4 * bytesPerPixel];
- long contentOffset = 131072 * bytesPerPixel;
- foreach (ImageList i in imageList)
- {
- array[0] = i.PixelWidth;
- array[1] = i.PixelHeight;
- array[2] = (byte)(contentOffset / bytesPerPixel >> 24);
- array[3] = (byte)(contentOffset / bytesPerPixel >> 16);
- array[4] = (byte)(contentOffset / bytesPerPixel >> 8);
- array[5] = (byte)(contentOffset / bytesPerPixel);
- array[6] = i.Pixels[0];
- array[7] = i.Pixels[1];
- //Header
- icoFile.Position = i.Index * 4 * bytesPerPixel; //header offset
- icoFile.Write(array, 0, 4 * bytesPerPixel);
- //Content
- icoFile.Position = contentOffset;
- icoFile.Write(i.Pixels, 0, i.PixelWidth * i.PixelHeight * bytesPerPixel);
- contentOffset += i.PixelWidth * i.PixelHeight * bytesPerPixel;
- }
- icoFile.Close();
- }
- private void DisplayDWIcoContent()
- {
- FileStream fs = new FileStream(icoFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
- if (fs.Length > 131072 * bytesPerPixel)
- {
- for (int i = 0; i <= 32767; i++)
- {
- byte[] array = new byte[4 * bytesPerPixel];
- fs.Position = i * 4 * bytesPerPixel;
- fs.Read(array, 0, 4 * bytesPerPixel);
- int width = array[0];
- int height = array[1];
- long offset = ((array[2] << 24) + (array[3] << 16) + (array[4] << 8) + array[5]) * bytesPerPixel;
- if (offset <= 0)
- {
- continue;
- }
- if (offset >= 131072 * bytesPerPixel)
- {
- DisPlayIco(fs, width, height, offset);
- Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() => { }));
- }
- }
- }
- fs.Close();
- }
- public void DisPlayIco(FileStream fs, int width, int height, long offset)
- {
- WriteableBitmap wbitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgr565, null);
- byte[,,] pixels = new byte[width, height, 2];
- byte[] pixels1d = new byte[height * width * bytesPerPixel];
- fs.Position = offset;
- byte[] array = new byte[bytesPerPixel];
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- fs.Read(array, 0, bytesPerPixel);
- pixels[j, i, 0] = array[1];
- pixels[j, i, 1] = array[0];
- }
- }
- int index = 0;
- for (int row = 0; row < height; row++)
- {
- for (int col = 0; col < width; col++)
- {
- for (int i = 0; i < bytesPerPixel; i++)
- {
- pixels1d[index++] = pixels[col, row, i];
- }
- }
- }
- Int32Rect rect = new Int32Rect(0, 0, width, height);
- int stride = (width * wbitmap.Format.BitsPerPixel + 7) / 8;
- wbitmap.WritePixels(rect, pixels1d, stride, 0);
- Image image = new Image()
- {
- Width = 270,
- Height = 270,
- Stretch = Stretch.None,
- Margin = new Thickness(0),
- Source = wbitmap
- };
- RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality);
- uxIcoUniformGrid.Children.Add(image);
- }
- private void uxBrowseButton_Click(object sender, RoutedEventArgs e)
- {
- var dlg = new CommonOpenFileDialog()
- {
- IsFolderPicker = true,
- Title = "Select image files path",
- };
- if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
- {
- imgFolderPath = dlg.FileName;
- uxImageBrowseTextBox.Text = imgFolderPath;
- GenerateImageList();
- }
- }
- private void uxRefreshButton_Click(object sender, RoutedEventArgs e)
- {
- if (Directory.Exists(imgFolderPath))
- {
- GenerateImageList();
- }
- }
- private void uxBuildButton_Click(object sender, RoutedEventArgs e)
- {
- uxIcoUniformGrid.Children.Clear();
- BuildIcoFile();
- DisplayDWIcoContent();
- }
- }
- }
|