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
{
///
/// BuildIco.xaml 的互動邏輯
///
public partial class BuildIco : UserControl
{
private string imgFolderPath;
private string icoFileName;
private List imageList;
private int bytesPerPixel = 2;
public BuildIco()
{
InitializeComponent();
imageList = new List();
}
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(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();
}
}
}