-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
364 lines (279 loc) · 10.8 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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
namespace MirrorFace
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
Mica = new SimulatedMica(this);
sourceImage.AllowDrop = true;
Text = Application.ProductName;
// Disable controls that don't work with no image loaded
copyOriginalToClipboardToolStripMenuItem.Enabled = saveOriginalAsToolStripMenuItem.Enabled = copyMirrorLeftBtn.Enabled = saveMirrorLeftBtn.Enabled = copyMirrorRightBtn.Enabled = saveMirrorRightBtn.Enabled = editImageMenuButton.Enabled = centerPosTrack.Enabled = false;
}
#region "Core functions"
private void LoadFromAI()
{
try
{
sourceImage.Load("https://thispersondoesnotexist.com/");
LoadImages();
} catch (Exception ex)
{
MessageBox.Show("Failed to load generated image.\nReason: " + ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LoadFromLocal()
{
openFileDialog.ShowDialog();
}
private void LoadFromClipboard()
{
if (Clipboard.ContainsImage())
{
sourceImage.Image = Clipboard.GetImage();
LoadImages();
} else
{
MessageBox.Show(this, "Error pasting image: Clipboard data is not an image.", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
sourceImage.Image = Image.FromFile(openFileDialog.FileName);
LoadImages();
}
private void FlipImageVertically(bool sideToFlip)
{
int sliceWidth = sideToFlip ? centerPosTrack.Value : (sourceImage.Image.Width - centerPosTrack.Value);
int sourceHeight = sourceImage.Image.Height;
Bitmap b = new Bitmap(sliceWidth * 2, sourceHeight, sourceImage.Image.PixelFormat);
if (sideToFlip)
mirrorLeftImage.Image = b;
else
mirrorRightImage.Image = b;
Graphics g = Graphics.FromImage(sideToFlip ? mirrorLeftImage.Image : mirrorRightImage.Image);
Bitmap b1 = new Bitmap(sourceImage.Image);
if (!sideToFlip)
{
b1.RotateFlip(RotateFlipType.RotateNoneFlipX);
}
Rectangle source = new Rectangle(0, 0, sliceWidth, sourceHeight);
Rectangle dest = new Rectangle(sliceWidth, 0, sliceWidth, sourceHeight);
Bitmap b2 = b1.Clone(source, sourceImage.Image.PixelFormat);
b2.RotateFlip(RotateFlipType.RotateNoneFlipX);
g.DrawImage(b1, source, source, GraphicsUnit.Pixel);
g.DrawImage(b2, dest, source, GraphicsUnit.Pixel);
b1.Dispose();
b2.Dispose();
}
private void FlipImages()
{
FlipImageVertically(true);
FlipImageVertically(false);
}
private void LoadImages()
{
copyOriginalToClipboardToolStripMenuItem.Enabled = saveOriginalAsToolStripMenuItem.Enabled = copyMirrorLeftBtn.Enabled = saveMirrorLeftBtn.Enabled = copyMirrorRightBtn.Enabled = saveMirrorRightBtn.Enabled = editImageMenuButton.Enabled = centerPosTrack.Enabled = true;
centerPosTrack.Maximum = sourceImage.Image.Width - 1;
centerPosTrack.Value = centerPosTrack.Maximum / 2;
FlipImages();
}
private int fileCounter = 1;
private void SaveImageAs(Image imageToSave)
{
saveFileDialog.FileName = $"Image {fileCounter}";
DialogResult result = saveFileDialog.ShowDialog();
if (result == DialogResult.OK)
{
fileCounter += 1;
ImageFormat saveFormat = ImageFormat.Bmp;
string fileName = saveFileDialog.FileName;
string fileExt = Path.GetExtension(fileName).ToLower();
if (fileExt == ".png")
saveFormat = ImageFormat.Png;
else if (fileExt == ".jpg" || fileExt == ".jpeg")
saveFormat = ImageFormat.Jpeg;
else if (fileExt == ".bmp")
saveFormat = ImageFormat.Bmp;
imageToSave.Save(fileName, saveFormat);
}
}
#endregion
#region "Position Track Bar"
private void centerPosTrack_Scroll(object sender, EventArgs e)
{
sourceImage.Invalidate();
FlipImages();
}
private void centerPosTrack_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
centerPosTrack.Value = centerPosTrack.Maximum / 2;
FlipImages();
}
}
private void sourceImage_Paint(object sender, PaintEventArgs e)
{
if (sourceImage.Image != null)
{
int lineX = (centerPosTrack.Value * sourceImage.Width) / sourceImage.Image.Width;
Point lineStart = new Point(lineX, 0);
Point lineEnd = new Point(lineX, sourceImage.Height);
Pen linePen = new Pen(Color.FromArgb(128, 255, 0, 0), 2);
e.Graphics.DrawLine(linePen, lineStart, lineEnd);
}
}
#endregion
#region "Button Events"
private void aboutBtn_Click(object sender, EventArgs e)
{
AboutDialog aboutDialog = new AboutDialog();
aboutDialog.ShowDialog();
}
#region "Left mirror"
private void copyMirrorLeftBtn_Click(object sender, EventArgs e)
{
Clipboard.SetImage(mirrorLeftImage.Image);
}
private void saveMirrorLeftBtn_Click(object sender, EventArgs e)
{
SaveImageAs(mirrorLeftImage.Image);
}
#endregion
#region "Right mirror"
private void copyMirrorRightBtn_Click(object sender, EventArgs e)
{
Clipboard.SetImage(mirrorRightImage.Image);
}
private void saveMirrorRightBtn_Click(object sender, EventArgs e)
{
SaveImageAs(mirrorRightImage.Image);
}
#endregion
#region "Menu open buttons"
private void loadImageMenuButton_Click(object sender, EventArgs e)
{
loadImageMenu.Show(loadImageMenuButton, new Point(0, 0), ToolStripDropDownDirection.AboveRight);
}
private void editImageMenuButton_Click(object sender, EventArgs e)
{
editImageMenu.Show(editImageMenuButton, new Point(0,0), ToolStripDropDownDirection.AboveRight);
}
#endregion
#region "Load menu"
private void copyOriginalToClipboardToolStripMenuItem_Click(object sender, EventArgs e)
{
Clipboard.SetImage(sourceImage.Image);
}
private void saveOriginalAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveImageAs(sourceImage.Image);
}
private void generatedByAIToolStripMenuItem_Click(object sender, EventArgs e)
{
LoadFromAI();
}
private void fromLocalImageToolStripMenuItem_Click(object sender, EventArgs e)
{
LoadFromLocal();
}
private void fromClipboardToolStripMenuItem_Click(object sender, EventArgs e)
{
LoadFromClipboard();
}
#endregion
#region "Edit menu"
private void rotateClockwiseToolStripMenuItem_Click(object sender, EventArgs e)
{
Bitmap b = new Bitmap(sourceImage.Image);
b.RotateFlip(RotateFlipType.Rotate90FlipNone);
sourceImage.Image = b;
LoadImages();
}
private void rotateCounterclockwiseToolStripMenuItem_Click(object sender, EventArgs e)
{
Bitmap b = new Bitmap(sourceImage.Image);
b.RotateFlip(RotateFlipType.Rotate270FlipNone);
sourceImage.Image = b;
LoadImages();
}
private void flipHorizontallyToolStripMenuItem_Click(object sender, EventArgs e)
{
Bitmap b = new Bitmap(sourceImage.Image);
b.RotateFlip(RotateFlipType.RotateNoneFlipX);
sourceImage.Image = b;
LoadImages();
}
private void flipVerticallyToolStripMenuItem_Click(object sender, EventArgs e)
{
Bitmap b = new Bitmap(sourceImage.Image);
b.RotateFlip(RotateFlipType.RotateNoneFlipY);
sourceImage.Image = b;
LoadImages();
}
#endregion
#endregion
#region "Simulated Mica effect"
SimulatedMica Mica;
private void MainForm_Shown(object sender, EventArgs e)
{
Mica.CaptureSetColor(true);
}
private void MainForm_Activated(object sender, EventArgs e)
{
Mica.CaptureSetColor(true);
}
private void MainForm_Deactivate(object sender, EventArgs e)
{
Mica.CaptureSetColor(true);
}
private void MainForm_BackColorChanged(object sender, EventArgs e)
{
// Set background color to menus
loadImageMenu.BackColor = editImageMenu.BackColor = BackColor;
}
#endregion
#region "Drag and drop to load image"
private bool IsImageFile(string filePath)
{
string extension = Path.GetExtension(filePath).ToLower();
return extension == ".jpg" || extension == ".jpeg" || extension == ".png" || extension == ".gif" || extension == ".bmp";
}
private void sourceImage_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] droppedFiles = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in droppedFiles)
{
if (IsImageFile(file))
{
try
{
Image i = Image.FromFile(file);
sourceImage.Image = i;
LoadImages();
} catch { }
}
}
}
}
private void sourceImage_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.All;
}
}
#endregion
}
}