-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
253 lines (204 loc) · 7.53 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows;
namespace IcoGenerator
{
public class FileInfo
{
public string FullFilename { get; set; }
public string Filename { get; set; }
public string SizeDisplay { get; set; }
public System.Drawing.Size Size { get; set; }
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private List<FileInfo> loadedFiles = new List<FileInfo>();
public MainWindow()
{
InitializeComponent();
AllowDrop = true;
}
/// <summary>
/// Test whether drag and drop is allowed
/// </summary>
protected override void OnDragOver(DragEventArgs e)
{
try
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
e.Effects = DragDropEffects.Move;
foreach (var file in files)
{
var ext = Path.GetExtension(file);
if (string.Compare(ext, ".png", true) != 0)
{
e.Effects = DragDropEffects.None;
break;
}
}
}
catch
{
e.Effects = DragDropEffects.None;
}
e.Handled = true;
}
/// <summary>
/// Accept dropping a file
/// </summary>
protected override void OnDrop(DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
AddFiles(files);
}
private void OnAddButtonClicked(object sender, RoutedEventArgs e)
{
var openDialog = new System.Windows.Forms.OpenFileDialog();
openDialog.Filter = "Image File (*.png)|*.png";
openDialog.Multiselect = true;
var result = openDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
AddFiles(openDialog.FileNames);
}
}
private void AddFiles(string[] filenames)
{
foreach (var file in filenames)
{
if (loadedFiles.Exists(x => x.FullFilename == file))
{
continue;
}
var size = GetImageSize(file);
if (size.Height > 256 || size.Width > 256)
{
continue;
}
var loaded = loadedFiles.Find(x => x.Size == size);
if (loaded != null)
{
loadedFiles.Remove(loaded);
}
var filename = Path.GetFileName(file);
loadedFiles.Add(new FileInfo { FullFilename = file, Filename = filename, Size = size, SizeDisplay = $"{size.Width}x{ size.Height }" });
}
loadedFiles.Sort((x, y) =>
{
if (x.Size.Width == y.Size.Width)
{
return 0;
}
return x.Size.Width < y.Size.Width ? -1 : 1;
});
FileList.ItemsSource = null;
FileList.ItemsSource = loadedFiles;
Hint.Visibility = Visibility.Collapsed;
}
private void OnClearButtonClicked(object sender, RoutedEventArgs e)
{
loadedFiles.Clear();
FileList.ItemsSource = null;
Hint.Visibility = Visibility.Visible;
}
private void OnExportButtonClicked(object sender, RoutedEventArgs e)
{
var saveDialog = new Microsoft.Win32.SaveFileDialog();
saveDialog.Filter = "ICO File (*.ico)|*.ico";
var result = saveDialog.ShowDialog();
if (result == true)
{
ExportIco(saveDialog.FileName);
}
}
private void ExportIco(string icoFilename)
{
try
{
FileStream outputFile = new FileStream(icoFilename, FileMode.Create);
BinaryWriter binWriter = new BinaryWriter(outputFile);
List<long> offsets = new List<long>();
// icon dir
// must be 0
binWriter.Write((short)0);
// type
binWriter.Write((short)1);
// # of images
binWriter.Write((short)loadedFiles.Count);
// ICONDIRENTRY
foreach (var file in loadedFiles)
{
// width
var width = file.Size.Width;
binWriter.Write((byte)(width == 256 ? 0 : width));
// height
var height = file.Size.Height;
binWriter.Write((byte)(height == 256 ? 0 : height));
// # colors in palette
binWriter.Write((byte)0);
// reserved => 0
binWriter.Write((byte)0);
// # color planes
binWriter.Write((short)1);
// # bit per pixel
binWriter.Write((short)0);
// remember file position - will be patched later
offsets.Add(outputFile.Position);
// size of the image
binWriter.Write((uint)0);
// offset of the image in file
binWriter.Write((uint)0);
}
int resIndex = 0;
foreach (var file in loadedFiles)
{
var startPosition = outputFile.Position;
using (var fileStream = new FileStream(file.FullFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var inputBitmap = Image.FromStream(fileStream);
Bitmap newBitmap = new Bitmap(inputBitmap);
using (MemoryStream memoryStream = new MemoryStream())
{
newBitmap.Save(memoryStream, ImageFormat.Png);
binWriter.Write(memoryStream.ToArray());
}
}
var currentPosition = outputFile.Position;
var size = currentPosition - startPosition;
outputFile.Position = offsets[resIndex];
// patch size
binWriter.Write((uint)size);
// patch offset of the image
binWriter.Write((uint)startPosition);
// set position back to the end
outputFile.Position = currentPosition;
resIndex++;
}
outputFile.Close();
outputFile.Dispose();
}
catch
{
}
}
private System.Drawing.Size GetImageSize(string imageFilename)
{
System.Drawing.Size size;
using (var fileStream = new FileStream(imageFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (var image = Image.FromStream(fileStream, false, false))
{
size = new System.Drawing.Size(image.Width, image.Height);
}
}
return size;
}
}
}