-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
490 lines (412 loc) · 17.2 KB
/
MainForm.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*********************************************************************************
This file is part of Imagizer2.
Imagizer2 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Imagizer2 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Imagizer2. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
using System;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Diagnostics;
namespace Imagizer2
{
public partial class MainForm : Form
{
private readonly EventProcessor.ConversionCompleteEventHandler _conversionCompleteHandler;
private readonly EventProcessor.SetProgressBarEventHandler _setProgressBarHandler;
private delegate void LocalDelegate();
private delegate void LocalDelegateParm(int val);
private int _startTicks;
private AspectLockState _aspectLockState;
public MainForm()
{
InitializeComponent();
this.Text = string.Format("Imagizer2 v{0}", Assembly.GetExecutingAssembly().GetName().Version.ToString());
txtThreadCount.Text = Environment.ProcessorCount.ToString();
DataContainer.Running = false;
LoadSettings();
_conversionCompleteHandler = new EventProcessor.ConversionCompleteEventHandler(Instance_ConversionComplete);
_setProgressBarHandler = new EventProcessor.SetProgressBarEventHandler(Instance_SetProgressBar);
EventProcessor.Instance.ConversionComplete += _conversionCompleteHandler;
EventProcessor.Instance.SetProgressBar += _setProgressBarHandler;
}
#region Methods
private void SetProgBar(int percent)
{
if (!this.IsDisposed && !DataContainer.Cancel)
this.Invoke((LocalDelegateParm)DelegateSetProgBar, percent);
}
private void DelegateSetProgBar(int percent)
{
progBar.Value = percent;
}
private void Reset()
{
if (!this.IsDisposed)
this.Invoke((LocalDelegate)DelegateReset);
}
private void DelegateReset()
{
btnGo.Text = "Start";
btnGo.Enabled = true;
gbThreadingSetup.Enabled = true;
progBar.Value = 0;
}
private void ConversionComplete()
{
if (DataContainer.Running)
{
int endTicks = Environment.TickCount;
SetProgBar(100);
MessageBox.Show("Complete!" + Environment.NewLine + DataContainer.TotalFiles.ToString() + " files in " + ((decimal)endTicks - (decimal)_startTicks) / 1000 + " seconds.");
Reset();
DataContainer.Running = false;
}
}
private void SetAspectLockbuttons(bool mode)
{
btnLockWidth.Enabled = mode;
btnLockHeight.Enabled = mode;
}
private void SetNoAspectLockMode()
{
btnLockHeight.Text = "L";
btnLockWidth.Text = "L";
txtHeight.Enabled = true;
txtWidth.Enabled = true;
_aspectLockState = AspectLockState.Disabled;
}
private void SetLockWidthMode()
{
if (_aspectLockState != AspectLockState.LockWidth)
{
btnLockHeight.Text = "L";
btnLockWidth.Text = "U";
txtHeight.Enabled = true;
txtWidth.Enabled = false;
_aspectLockState = AspectLockState.LockWidth;
}
else
{
SetNoAspectLockMode();
}
}
private void SetLockHeightMode()
{
if (_aspectLockState != AspectLockState.LockHeight)
{
btnLockHeight.Text = "U";
btnLockWidth.Text = "L";
txtWidth.Enabled = true;
txtHeight.Enabled = false;
_aspectLockState = AspectLockState.LockHeight;
}
else
{
SetNoAspectLockMode();
}
}
private void txtThreadCount_TextChanged(object sender, EventArgs e)
{
try
{
sbThreads.Value = int.Parse(txtThreadCount.Text);
}
catch { }
}
private void SelectDirectoryToTextBox(Control textBox, string title, bool showCreateDirButton)
{
using (FolderBrowserDialog fbDialog = new FolderBrowserDialog())
{
fbDialog.ShowNewFolderButton = showCreateDirButton;
fbDialog.SelectedPath = textBox.Text;
fbDialog.Description = title;
if (fbDialog.ShowDialog(this) == DialogResult.OK)
textBox.Text = fbDialog.SelectedPath;
}
}
private void DoStartConversion()
{
if (DataContainer.Running)
{
MessageBox.Show("Already Running!", "Imagizer2 Error!");
return;
}
ConversionParameters conversionParameters;
try
{
conversionParameters = GetConversionParameters();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
gbThreadingSetup.Enabled = false;
btnGo.Text = "Working";
btnGo.Enabled = false;
if (!Directory.Exists(conversionParameters.OutputDir))
Directory.CreateDirectory(conversionParameters.OutputDir);
_startTicks = Environment.TickCount;
ImgProcessor.RunImagizer(conversionParameters);
}
private void ProcessExit()
{
DataContainer.Cancel = true;
EventProcessor.Instance.ConversionComplete -= _conversionCompleteHandler;
EventProcessor.Instance.SetProgressBar -= _setProgressBarHandler;
if (DataContainer.FileList != null && DataContainer.FileList.Count > 0)
DataContainer.FileList.Clear();
try
{
ConversionParameters conversionParameters = GetConversionParameters();
ImagizerSettings.Default.SettingXml = SerializationUtility.SerializeObject(conversionParameters);
ImagizerSettings.Default.Save();
}
catch (Exception ex)
{
Debug.Print(ex.ToString());
//we threw ourselves an error, so we will not continue.
//A messagebox should have already been displayed
return;
}
}
#endregion
#region Event Handlers
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
DataContainer.Cancel = true;
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
ProcessExit();
}
private void rbPercent_CheckedChanged(object sender, EventArgs e)
{
if (rbPercent.Checked)
{
SetAspectLockbuttons(false);
SetNoAspectLockMode();
}
}
private void rbPixels_CheckedChanged(object sender, EventArgs e)
{
if (rbPixels.Checked)
{
SetAspectLockbuttons(true);
}
}
void Instance_SetProgressBar(object sender, int percent)
{
SetProgBar(percent);
}
void Instance_ConversionComplete(object sender)
{
ConversionComplete();
}
private void cbFormat_CheckedChanged(object sender, EventArgs e)
{
gbFormat.Enabled = cbFormat.Checked;
}
private void cbSize_CheckedChanged(object sender, EventArgs e)
{
gbSize.Enabled = cbSize.Checked;
}
private void btnInputBrowse_Click(object sender, EventArgs e)
{
SelectDirectoryToTextBox(txtInputDir, "Select the source directory", false);
}
private void btnOutputBrowse_Click(object sender, EventArgs e)
{
SelectDirectoryToTextBox(txtOutputDir, "Select the destination directory", true);
}
private void sbThreads_Scroll(object sender, ScrollEventArgs e)
{
txtThreadCount.Text = sbThreads.Value.ToString();
}
private void btnLockWidth_Click(object sender, EventArgs e)
{
SetLockWidthMode();
}
private void btnLockHeight_Click(object sender, EventArgs e)
{
SetLockHeightMode();
}
private void btnGo_Click(object sender, EventArgs e)
{
if (txtInputDir.Text == string.Empty || txtOutputDir.Text == string.Empty || txtInputDir.Text == txtOutputDir.Text)
{
throw new ApplicationException(string.Format("You must fill out both the input and output folder,{0}and they have to be different folders,{0}they cannot point to the same place.", Environment.NewLine));
}
else
{
DoStartConversion();
}
}
#endregion
#region Settings Load Save
private void LoadSettings()
{
ConversionParameters conversionParameters;
if (string.IsNullOrEmpty(ImagizerSettings.Default.SettingXml))
return;
try
{
conversionParameters = SerializationUtility.DeserializeObject<ConversionParameters>(ImagizerSettings.Default.SettingXml);
SetConversionParameters(conversionParameters);
}
catch
{
//just toss it here because it probably means our settings are blank or corrupt
}
}
/// <summary>
/// Reads parameters from application (mostly the form) and creates a
/// ConversionParameters which can then be serialized into a settings file
/// </summary>
/// <returns></returns>
private ConversionParameters GetConversionParameters()
{
ConversionParameters conversionParameters = new ConversionParameters();
conversionParameters.InputDir = txtInputDir.Text;
conversionParameters.OutputDir = txtOutputDir.Text;
conversionParameters.AspectLockState = _aspectLockState;
conversionParameters.NumThreads = int.Parse(txtThreadCount.Text);
conversionParameters.IncludeSubDirs = cbRecusive.Checked;
conversionParameters.StripExif = cbStripExif.Checked;
if (cbFormat.Checked)
{
if (rbJpeg.Checked)
{
conversionParameters.ImageFormat = ImageFormat.Jpeg;
conversionParameters.NewExtention = ".jpg";
}
else if (rbGif.Checked)
{
conversionParameters.ImageFormat = ImageFormat.Gif;
conversionParameters.NewExtention = ".gif";
}
else if (rbBmp.Checked)
{
conversionParameters.ImageFormat = ImageFormat.Bmp;
conversionParameters.NewExtention = ".bmp";
}
else if (rbPng.Checked)
{
conversionParameters.ImageFormat = ImageFormat.Png;
conversionParameters.NewExtention = ".png";
}
else if (rbTiff.Checked)
{
conversionParameters.ImageFormat = ImageFormat.Tiff;
conversionParameters.NewExtention = ".tiff";
}
else if (rbWmf.Checked)
{
conversionParameters.ImageFormat = ImageFormat.Wmf;
conversionParameters.NewExtention = ".wmf";
}
else if (rbExif.Checked)
{
conversionParameters.ImageFormat = ImageFormat.Exif;
conversionParameters.NewExtention = ".exif";
}
else if (rbEmf.Checked)
{
conversionParameters.ImageFormat = ImageFormat.Emf;
conversionParameters.NewExtention = ".emf";
}
else if (rbIco.Checked)
{
conversionParameters.ImageFormat = ImageFormat.Icon;
conversionParameters.NewExtention = ".ico";
}
}
try
{
if (cbSize.Checked)
{
conversionParameters.NewHeight = int.Parse(txtHeight.Text);
conversionParameters.NewWidth = int.Parse(txtWidth.Text);
if (rbPixels.Checked)
conversionParameters.ResizeMode = ResizeMode.Pixels;
else if (cbSize.Checked && rbPercent.Checked)
conversionParameters.ResizeMode = ResizeMode.Percent;
}
}
catch (Exception ex)
{
throw new ApplicationException(string.Format("Make sure there are only numbers and not letters or{0}something in the height and width boxes.", Environment.NewLine), ex);
}
return conversionParameters;
}
/// <summary>
/// Takes a ConversionParameters and loads all of the settings (mostly to the form)
/// This is typically used after de-serializing from a settings file
/// </summary>
/// <param name="conversionParameters">ConversionParameters object</param>
private void SetConversionParameters(ConversionParameters conversionParameters)
{
txtThreadCount.Text = conversionParameters.NumThreads.ToString();
txtInputDir.Text = conversionParameters.InputDir;
txtOutputDir.Text = conversionParameters.OutputDir;
_aspectLockState = AspectLockState.Disabled;
txtHeight.Text = conversionParameters.NewHeight.ToString();
txtWidth.Text = conversionParameters.NewWidth.ToString();
cbRecusive.Checked = conversionParameters.IncludeSubDirs;
cbStripExif.Checked = conversionParameters.StripExif;
rbPercent.Checked = false;
rbPixels.Checked = false;
switch (conversionParameters.AspectLockState)
{
case AspectLockState.LockHeight:
SetLockHeightMode();
break;
case AspectLockState.LockWidth:
SetLockWidthMode();
break;
}
if (conversionParameters.ImageFormat != null && conversionParameters.NewExtention != null)
cbFormat.Checked = true;
if (conversionParameters.ImageFormat == ImageFormat.Jpeg)
rbJpeg.Checked = true;
else if (conversionParameters.ImageFormat == ImageFormat.Gif)
rbGif.Checked = true;
else if (conversionParameters.ImageFormat == ImageFormat.Bmp)
rbBmp.Checked = true;
else if (conversionParameters.ImageFormat == ImageFormat.Png)
rbPng.Checked = true;
else if (conversionParameters.ImageFormat == ImageFormat.Tiff)
rbTiff.Checked = true;
else if (conversionParameters.ImageFormat == ImageFormat.Wmf)
rbWmf.Checked = true;
else if (conversionParameters.ImageFormat == ImageFormat.Exif)
rbExif.Checked = true;
else if (conversionParameters.ImageFormat == ImageFormat.Emf)
rbEmf.Checked = true;
else if (conversionParameters.ImageFormat == ImageFormat.Icon)
rbIco.Checked = true;
switch (conversionParameters.ResizeMode)
{
case ResizeMode.Percent:
rbPercent.Checked = true;
cbSize.Checked = true;
break;
case ResizeMode.Pixels:
rbPixels.Checked = true;
cbSize.Checked = true;
break;
}
}
#endregion
}
}