-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
348 lines (314 loc) · 13.3 KB
/
MainForm.cs
File metadata and controls
348 lines (314 loc) · 13.3 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
using IconLib;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading.Tasks;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace FaviconGenerator
{
public class MainForm : Form
{
// 在類別開頭新增欄位
private bool _isGenerating = false;
// 控制項宣告
private TextBox txtSource;
private TextBox txtDestination;
private ComboBox cmbSizes;
private Button btnBrowseSource;
private Button btnBrowseDest;
private Button btnGenerate;
private Button btnExit;
private Button btnHelp;
// 預定義尺寸
private readonly int[] allSizes = { 16, 24, 32, 48, 64, 96, 128, 256 };
public MainForm()
{
// 基本表單設定
this.Text = "Favicon 生成工具 (.NET)";
this.ClientSize = new Size(500, 180);
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
// 1. 第一行:來源檔案
var lblSource = new Label
{
Text = "來源檔案:",
Location = new Point(10, 15),
Width = 80
};
txtSource = new TextBox
{
Location = new Point(100, 12),
Width = 300
};
btnBrowseSource = new Button
{
Text = "瀏覽...",
Location = new Point(410, 10),
Width = 70
};
btnBrowseSource.Click += new EventHandler(BrowseSource_Click);
// 2. 第二行:目的地目錄
var lblDest = new Label
{
Text = "輸出目錄:",
Location = new Point(10, 45),
Width = 80
};
txtDestination = new TextBox
{
Location = new Point(100, 42),
Width = 300
};
btnBrowseDest = new Button
{
Text = "瀏覽...",
Location = new Point(410, 40),
Width = 70
};
btnBrowseDest.Click += new EventHandler(BrowseDest_Click);
// 3. 第三行:尺寸選擇
var lblSize = new Label
{
Text = "輸出選項:",
Location = new Point(10, 75),
Width = 80
};
cmbSizes = new ComboBox
{
Location = new Point(100, 72),
Width = 380,
DropDownStyle = ComboBoxStyle.DropDownList
};
cmbSizes.Items.AddRange(new object[] {
"全尺寸 ICO (包含所有標準尺寸)", // 對應 Python 的 'all' 參數
"全尺寸 ICO + 獨立 PNG", // 對應 Python 的 'all+' 參數
"16x16 (標準)",
"32x32 (標準)",
"48x48 (標準)",
"自訂尺寸..."
});
cmbSizes.SelectedIndex = 0; // 預設選中全尺寸
// 4. 第四行:功能按鈕
btnGenerate = new Button
{
Text = "執行生成",
Location = new Point(100, 110),
Width = 100,
BackColor = Color.LightGreen
};
btnGenerate.Click += new EventHandler(btnGenerate_Click); // 非同步版本
btnExit = new Button
{
Text = "結束",
Location = new Point(210, 110),
Width = 100
};
btnExit.Click += delegate(object s, EventArgs e) { this.Close(); };
btnHelp = new Button
{
Text = "幫助",
Location = new Point(320, 110),
Width = 100
};
btnHelp.Click += new EventHandler(ShowHelp);
// 將所有控制項加入表單
this.Controls.AddRange(new Control[] {
lblSource, txtSource, btnBrowseSource,
lblDest, txtDestination, btnBrowseDest,
lblSize, cmbSizes,
btnGenerate, btnExit, btnHelp
});
}
private void BrowseSource_Click(object sender, EventArgs e)
{
using (var dialog = new OpenFileDialog())
{
dialog.Filter = "PNG 圖片|*.png|所有檔案|*.*";
if (dialog.ShowDialog() == DialogResult.OK)
{
txtSource.Text = dialog.FileName;
if (string.IsNullOrEmpty(txtDestination.Text))
{
txtDestination.Text = Path.GetDirectoryName(dialog.FileName);
}
}
}
}
private void BrowseDest_Click(object sender, EventArgs e)
{
using (var dialog = new FolderBrowserDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
txtDestination.Text = dialog.SelectedPath;
}
}
}
// 修改按鈕事件處理方法
private async void btnGenerate_Click(object sender, EventArgs e)
{
if (_isGenerating) return;
_isGenerating = true;
btnGenerate.Enabled = false;
btnGenerate.Text = "生成中...";
btnGenerate.BackColor = Color.LightGray;
try
{
await Task.Factory.StartNew(() =>
{
// 將原有同步程式碼移至此處
if (string.IsNullOrEmpty(txtSource.Text) || !File.Exists(txtSource.Text))
{
MessageBox.Show("請選擇有效的來源檔案!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string baseName = Path.GetFileNameWithoutExtension(txtSource.Text);
string outputDir = txtDestination.Text;
// 根據選擇的選項決定處理方式
switch (cmbSizes.SelectedIndex)
{
case 0: // 全尺寸 ICO
GenerateIcoFile(txtSource.Text, outputDir, baseName, allSizes);
break;
case 1: // 全尺寸 ICO + PNG
GenerateIcoFile(txtSource.Text, outputDir, baseName, allSizes);
GeneratePngFiles(txtSource.Text, outputDir, baseName, allSizes);
break;
case 2: // 16x16
GenerateIcoFile(txtSource.Text, outputDir, baseName, new[] { 16 });
break;
case 3: // 32x32
GenerateIcoFile(txtSource.Text, outputDir, baseName, new[] { 32 });
break;
case 4: // 48x48
GenerateIcoFile(txtSource.Text, outputDir, baseName, new[] { 48 });
break;
case 5: // 自訂尺寸
HandleCustomSize(txtSource.Text, outputDir, baseName);
break;
}
});
MessageBox.Show("Favicon 生成完成!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (UnauthorizedAccessException)
{
MessageBox.Show("無法寫入輸出目錄,請檢查權限!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (OutOfMemoryException)
{
MessageBox.Show("圖片尺寸過大,請減少尺寸或使用較小的原始圖檔!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentException ex)
{
if (ex.Message.Contains("Parameter is not valid"))
{
MessageBox.Show("不支援的圖片格式,請使用 PNG 或其他標準格式!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show(string.Format("生成失敗: {0}\n{1}", ex.GetType().Name, ex.Message), "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(string.Format("生成失敗: {0}\n{1}", ex.GetType().Name, ex.Message), "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
btnGenerate.Enabled = true;
btnGenerate.Text = "執行生成";
btnGenerate.BackColor = Color.LightGreen;
_isGenerating = false;
}
}
private void GenerateIcoFile(string sourcePath, string outputDir, string baseName, IEnumerable<int> sizes)
{
// 確保輸出目錄存在
if (!Directory.Exists(outputDir))
{
Directory.CreateDirectory(outputDir);
}
string icoPath = Path.Combine(outputDir, string.Format("{0}.ico", baseName));
using (Image original = Image.FromFile(sourcePath))
{
using (MultiIcon multiIcon = new MultiIcon())
{
IconImage icon = multiIcon.Add("Favicon"); // 圖標名稱(可自訂)
foreach (int size in sizes)
{
using (Bitmap resized = new Bitmap(size, size, PixelFormat.Format32bppArgb)) // 新增 PixelFormat
{
using (Graphics g = Graphics.FromImage(resized))
{
g.Clear(Color.Transparent); // 清除為透明背景
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(original, 0, 0, size, size);
}
icon.Add(resized);
}
}
// 儲存為 ICO 檔案
multiIcon.Save(icoPath, MultiIconFormat.ICO);
}
}
}
private void GeneratePngFiles(string sourcePath, string outputDir, string baseName, IEnumerable<int> sizes)
{
using (Image original = Image.FromFile(sourcePath))
{
foreach (int size in sizes)
{
using (Bitmap resized = new Bitmap(original, new Size(size, size)))
{
string pngPath = Path.Combine(outputDir, string.Format("{0}_{1}x{1}.png", baseName, size));
resized.Save(pngPath, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
}
private void HandleCustomSize(string sourcePath, string outputDir, string baseName)
{
using (InputDialog dialog = new InputDialog("自訂尺寸", "請輸入尺寸 (多個尺寸用逗號分隔):", "16,32,48"))
{
if (dialog.ShowDialog() == DialogResult.OK)
{
string[] parts = dialog.InputText.Split(',');
List<int> customSizes = new List<int>();
foreach (string s in parts)
{
int size;
if (int.TryParse(s.Trim(), out size) && size > 0)
{
customSizes.Add(size);
}
}
if (customSizes.Count == 0)
{
MessageBox.Show("無效的尺寸輸入!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
GenerateIcoFile(sourcePath, outputDir, baseName, customSizes.ToArray());
}
}
}
private void ShowHelp(object sender, EventArgs e)
{
MessageBox.Show(
"Favicon 生成工具使用說明:\n\n" +
"1. 選擇來源 PNG 圖片\n" +
"2. 指定輸出目錄\n" +
"3. 選擇輸出選項:\n" +
" - 全尺寸 ICO: 生成包含所有標準尺寸的單一 .ico 文件\n" +
" - 全尺寸 ICO + PNG: 同時生成 .ico 和各尺寸的 .png\n" +
" - 單一尺寸: 只生成指定尺寸的 .ico\n" +
" - 自訂尺寸: 手動輸入多個尺寸\n\n" +
"標準尺寸包含: 16, 24, 32, 48, 64, 96, 128, 256 像素",
"幫助",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}