-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertor.cs
159 lines (133 loc) · 5.53 KB
/
Convertor.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
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
namespace Convertor
{
public partial class Convertor : Form
{
private const int THREAD_QUEUE = 16;
private const int THREAD_QUEUE_SLEEP = 20; // in milliseconds
private const int STATUS_UPDATE_INTERVAL = 300; // in milliseconds
public Convertor()
{
InitializeComponent();
ToFormatComboBox.SelectedIndex = 0;
FromExtensionComboBox.SelectedIndex = 0;
}
private void CovertButton_Click(object sender, EventArgs e)
{
if(string.IsNullOrWhiteSpace(textBox1.Text))
{
MessageBox.Show("Choose Folder Path", "Error");
return;
}
if(!Directory.Exists(this.textBox1.Text))
{
MessageBox.Show("Incorrect Folder Path", "Error");
return;
}
string sourceFolderPath = textBox1.Text;
string convertedFolderPath = sourceFolderPath + "\\converted";
string[] sourceFiles = Directory.GetFiles(sourceFolderPath);
if(SaveRadioConvertedFolder.Checked)
{
Directory.CreateDirectory(convertedFolderPath);
}
statusLabel.Visible = true;
progressBar.Visible = true;
progressBar.Maximum = sourceFiles.Length;
progressBar.Value = 0;
ConvertPanel.Enabled = false;
string toFormatName = (string)ToFormatComboBox.SelectedItem;
ImageFormat toFormat = ImageConvertor.GetImageFormatByString(toFormatName);
string fromExtension = FromExtensionComboBox.SelectedItem.ToString().ToLower();
LinkedList<Thread> threadList = new LinkedList<Thread>();
DateTime lastIntervalTime = DateTime.Now;
statusLabel.Text = "Converting";
int count = 0;
int convertedCount = 0;
for(int index = 0; index < sourceFiles.Length; index++)
{
string sourceFilePath = sourceFiles[index];
string resultFilePath;
// Form status and progressBar update
void FormUpdate()
{
progressBar.Value = index;
if(DateTime.Now.Subtract(lastIntervalTime).Milliseconds > STATUS_UPDATE_INTERVAL)
{
statusLabel.Text = "Converting" + String.Concat(Enumerable.Repeat(".", (++count % 5)));
lastIntervalTime = DateTime.Now;
}
Application.DoEvents();
}
FormUpdate();
// continue if the source file extension is not the one I'm looking for (.jpg != .bmp)
if(!Path.GetExtension(sourceFilePath).ToLower().Equals("." + fromExtension))
{
continue;
}
convertedCount++;
resultFilePath = SaveRadioConvertedFolder.Checked ? convertedFolderPath : sourceFolderPath;
resultFilePath += "\\" + Path.GetFileNameWithoutExtension(sourceFilePath) + "." + toFormatName.ToLower();
ImageConvertor imageConvertor = new ImageConvertor(sourceFilePath, resultFilePath, toFormat);
// using sequential io = 430 bmp size 5.93MB to jpg, 42 secs on SSD , 242 secs on HDD
// using multi-threaded io = 430 bmp size 5.93MB to jpg, 14 secs on SSD , 122 secs on HDD
threadList.AddLast(new Thread(new ThreadStart(imageConvertor.ConvertImage)));
threadList.Last.Value.Start();
while(threadList.Count >= THREAD_QUEUE) // Thread Queue, the longest operation is IO so IO Queue
{
Thread.Sleep(THREAD_QUEUE_SLEEP);
FormUpdate();
RemoveNonLivingThreads(threadList);
}
}
foreach(Thread th in threadList)
{
th.Join(); // wait for the last threads to finish
}
MessageBox.Show("Converted " + convertedCount + " Files", "Conversion Finished");
statusLabel.Visible = false;
progressBar.Visible = false;
ConvertPanel.Enabled = true;
}
private void SelectFilesButton_Click(object sender, EventArgs e)
{
if(folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = folderBrowserDialog1.SelectedPath;
}
}
private void OpenButton_Click(object sender, EventArgs e)
{
if(Directory.Exists(this.textBox1.Text))
{
System.Diagnostics.Process.Start(this.textBox1.Text, null);
}
else
{
MessageBox.Show("Incorrect Folder Path", "Error");
}
}
private void RemoveNonLivingThreads(LinkedList<Thread> threadList)
{
LinkedListNode<Thread> node = threadList.First;
while(node != null && node.Next != null)
{
node = node.Next;
if(!node.Previous.Value.IsAlive)
{
threadList.Remove(node.Previous);
}
}
if(node != null && !node.Value.IsAlive)
{
threadList.Remove(node);
}
}
}
}