This repository has been archived by the owner on Aug 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFrmMain.cs
178 lines (164 loc) · 6.38 KB
/
FrmMain.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
namespace ImageConverter
{
public partial class FrmMain : Form
{
public Thread thread;
public bool running = false;
public int current;
public int total;
public Image source = null;
public Size SmallImageSize { get { return new Size((int)numWidth.Value, (int)numHeight.Value); } }
public FrmMain()
{
InitializeComponent();
comboExtension.SelectedIndex = 6;
}
private void btnLoad_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Image Splitter - Load Source Image";
ofd.Filter = "All Images|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff|Bitmap|*.bmp|Graphical Interchange Format|*.gif|JPEG|*.jpeg;*.jpg|Portable Network Graphic|*.png|Tagged Image File Format|*.tiff";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
try
{
source = Image.FromFile(ofd.FileName);
txtLoad.Text = ofd.FileName;
}
catch
{
MessageBox.Show("Unable to load the image!", "Image Splitter - Error");
txtLoad.Text = "";
source = null;
}
}
}
private void btnSave_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "Location to where the small images should be stored.";
fbd.ShowNewFolderButton = true;
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtSave.Text = fbd.SelectedPath + @"\";
}
}
private void btnStart_Click(object sender, EventArgs e)
{
if (source == null)
{
MessageBox.Show("Please enter a valid source!", "Image Splitter - Error");
return;
}
if (txtSave.Text == "")
{
MessageBox.Show("Please enter a valid save location!", "Image Splitter - Error");
return;
}
if (!running)
{
running = true;
btnStart.Text = "Stop!";
numHeight.ReadOnly = true;
numWidth.ReadOnly = true;
btnSave.Enabled = false;
btnLoad.Enabled = false;
thread = new Thread(new ParameterizedThreadStart(Action));
thread.Start(comboExtension.SelectedItem);
}
else
{
running = false;
thread.Abort();
btnStart.Text = "Start!";
numHeight.ReadOnly = false;
numWidth.ReadOnly = false;
btnSave.Enabled = true;
btnLoad.Enabled = true;
}
}
public void Action(object ext)
{
string extension = ext.ToString();
current = 0;
Size newSize = new Size((int)numWidth.Value, (int)numHeight.Value);
int rows = (source.Width / (int)numWidth.Value);
int columns = (source.Height / (int)numHeight.Value);
total = rows * columns;
for (int j = 0; j < rows; j++)
{
for (int i = 0; i < columns; i++)
{
Image curImage = new Bitmap(newSize.Width, newSize.Height);
int x = i * newSize.Height;
int y = j * newSize.Width;
Rectangle rect = new Rectangle(new Point(x, y), newSize);
using (Graphics g = Graphics.FromImage(curImage))
{
g.DrawImage(source, 0, 0, rect, GraphicsUnit.Pixel);
}
curImage.Save(txtSave.Text + txtFilename.Text.Replace("{num}", current.ToString()) + "." + extension, stringToFormat(extension));
current++;
this.Invoke((MethodInvoker)delegate
{
this.Text = "Image Splitter (" + current.ToString() + "/" + total.ToString() + ")";
});
}
}
this.Invoke((MethodInvoker)delegate
{
running = false;
btnStart.Text = "Start!";
numHeight.ReadOnly = false;
numWidth.ReadOnly = false;
btnSave.Enabled = true;
btnLoad.Enabled = true;
this.Text = "Image Splitter";
if (MessageBox.Show("Finished! Splitting Image.\n Want to open location?", "Image Splitter - Finished!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
Process.Start(txtSave.Text);
});
}
private void FrmMain_HelpButtonClicked(object sender, CancelEventArgs e)
{
e.Cancel = true;
MessageBox.Show("Image Splitter © 2012 Visual Bounds\n\nThis tool will allow you to split a single image into\n multiple smaller images of a predefined size.\n\nThis tool is free to use and open sourced.", "Image Spliter - About");
}
public ImageFormat stringToFormat(string str)
{
switch (str)
{
case "bmp":
return ImageFormat.Bmp;
case "emf":
return ImageFormat.Emf;
case "exif":
return ImageFormat.Exif;
case "gif":
return ImageFormat.Gif;
case "icon":
return ImageFormat.Icon;
case "jpeg":
return ImageFormat.Jpeg;
case "png":
return ImageFormat.Png;
case "tiff":
return ImageFormat.Tiff;
case "wmf":
return ImageFormat.Wmf;
default:
return ImageFormat.Png;
}
}
}
}