-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
464 lines (401 loc) · 18.6 KB
/
Copy pathForm1.cs
File metadata and controls
464 lines (401 loc) · 18.6 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
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
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace GraphEditor
{
public enum CoordinateState
{
Coordinates,
CalStart,
CalEnd,
Units
}
public partial class Form1 : Form
{
public IntPtr hookId = IntPtr.Zero;
// Colors
readonly Color DarkBG = Color.FromArgb(26, 26, 26);
readonly Color ObjectBG = Color.FromArgb(31, 31, 31);
// AlignImageView sizes
readonly Size DefaultAlignSize = new Size(205, 205);
readonly Size LargeAlignSize = new Size(410, 410);
// Image Directory
string imageFilePath { set; get; }
// Current Image
Image currentImage { set; get; }
bool ImageLoaded { set; get; }
// Zoom view image
Bitmap zoomBMP = new Bitmap(205, 205);
CoordinateState CurrentCoordinateState;
Point StartEdge { set; get; }
Point EndEdge { set; get; }
int GraphWidth
{
get
{
return Math.Abs(EndEdge.X - StartEdge.X);
}
}
int GraphHeight
{
get
{
return Math.Abs(EndEdge.Y - StartEdge.Y);
}
}
bool AllAxisParametersValid
{
get
{
return double.TryParse(xAxisStartValueBox.Text, out _)
&& double.TryParse(xAxisEndValueBox.Text, out _)
&& double.TryParse(yAxisStartValueBox.Text, out _)
&& double.TryParse(yAxisEndValueBox.Text, out _);
}
}
// Darkmode title bar
// Source: https://stackoverflow.com/a/64927217
[DllImport("DwmApi")]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize);
public const int WH_KEYBOARD_LL = 13;
public const int WM_KEYDOWN = 0x0100;
public const int WM_KEYUP = 0x0101;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
public IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}
protected override void OnHandleCreated(EventArgs e)
{
if (DwmSetWindowAttribute(Handle, 19, new[] { 1 }, 4) != 0)
DwmSetWindowAttribute(Handle, 20, new[] { 1 }, 4);
}
public Form1()
{
InitializeComponent();
// Center Picture Box
PicturePanel_Resize(this, new EventArgs());
// Hide coordinate label and image align view
CoordinateLabel.Visible = false;
AlignImageView.Visible = false;
// No image loaded yet
ImageLoaded = false;
CurrentCoordinateState = CoordinateState.Coordinates;
}
bool IsPointInGraphArea(Point testPoint)
{
bool isInXRange = testPoint.X >= Math.Min(StartEdge.X, EndEdge.X) && testPoint.X <= Math.Max(StartEdge.X, EndEdge.X);
bool isInYRange = testPoint.Y >= Math.Min(StartEdge.Y, EndEdge.Y) && testPoint.Y <= Math.Max(StartEdge.Y, EndEdge.Y);
return isInXRange && isInYRange;
}
Point PointToGraph(Point p)
{
return new Point(Math.Abs(p.X - StartEdge.X), Math.Abs(p.Y - StartEdge.Y));
}
// Source: https://stackoverflow.com/questions/374316/round-a-double-to-x-significant-figures
static double RoundToSignificantDigits(double d, int digits)
{
if (d == 0)
return 0;
double scale = Math.Pow(10, Math.Floor(Math.Log10(Math.Abs(d))) + 1);
return scale * Math.Round(d / scale, digits);
}
public IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
int vkCode = Marshal.ReadInt32(lParam);
if ((Keys)vkCode == Keys.LShiftKey || (Keys)vkCode == Keys.RShiftKey)
{
if (wParam == (IntPtr)WM_KEYDOWN)
{
AlignImageView.Size = LargeAlignSize;
AlignImageView.Location = new Point(12, splitContainer1.Size.Height - 12 - AlignImageView.Size.Height);
}
else if (wParam == (IntPtr)WM_KEYUP)
{
AlignImageView.Size = DefaultAlignSize;
AlignImageView.Location = new Point(12, splitContainer1.Size.Height - 12 - AlignImageView.Size.Height);
}
}
}
return CallNextHookEx(hookId, nCode, wParam, lParam);
}
private void mainPictureBox_DoubleClick(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp;*.gif";
openFileDialog.Title = "Select an Image File";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// Save file path
imageFilePath = openFileDialog.FileName;
// Load image
currentImage = Image.FromFile(imageFilePath);
// Load into PictureBox
mainPictureBox.Image = currentImage;
// Enable controls
bSave.Enabled = true;
bCalibrate.Enabled = true;
ImageLoaded = true;
PicturePanel_Resize(this, new EventArgs());
}
}
}
private void PicturePanel_Resize(object sender, EventArgs e)
{
Point PictureBoxPosition = mainPictureBox.Location;
if (PicturePanel.Width < mainPictureBox.Width)
{
PictureBoxPosition.X = 0;
}
else
{
PictureBoxPosition.X = PicturePanel.Width / 2 - mainPictureBox.Width / 2;
}
if (PicturePanel.Height < mainPictureBox.Height)
{
PictureBoxPosition.Y = 0;
}
else
{
PictureBoxPosition.Y = PicturePanel.Height / 2 - mainPictureBox.Height / 2;
}
mainPictureBox.Location = PictureBoxPosition;
}
private void bSave_Click(object sender, EventArgs e)
{
string directory = Path.GetDirectoryName(imageFilePath);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(imageFilePath);
string extension = Path.GetExtension(imageFilePath);
string editedFileName = $"{fileNameWithoutExtension}_edited{extension}";
string editedFilePath = Path.Combine(directory, editedFileName);
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.InitialDirectory = directory;
saveFileDialog.FileName = editedFileName;
saveFileDialog.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp;*.gif";
saveFileDialog.Title = "Save Edited Image";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
// Save the file (example: save the PictureBox image)
currentImage.Save(saveFileDialog.FileName);
}
}
}
private void bCalibrate_Click(object sender, EventArgs e)
{
switch (CurrentCoordinateState)
{
case CoordinateState.Coordinates:
case CoordinateState.Units:
CurrentCoordinateState = CoordinateState.CalStart;
break;
}
}
private void mainPictureBox_MouseEnter(object sender, EventArgs e)
{
CoordinateLabel.Visible = true;
if (ImageLoaded) AlignImageView.Visible = true;
}
private void mainPictureBox_MouseLeave(object sender, EventArgs e)
{
if ((CurrentCoordinateState == CoordinateState.Coordinates) || (CurrentCoordinateState == CoordinateState.Units))
{
CoordinateLabel.Visible = false;
AlignImageView.Visible = false;
}
else if (CurrentCoordinateState == CoordinateState.CalStart)
{
CoordinateLabel.ForeColor = SystemColors.Control;
CoordinateLabel.Text = "Select Edge Location with lowest Value";
}
else if (CurrentCoordinateState == CoordinateState.CalEnd)
{
CoordinateLabel.ForeColor = SystemColors.Control;
CoordinateLabel.Text = "Select Edge Location with highest Value";
}
}
private void mainPictureBox_MouseMove(object sender, MouseEventArgs e)
{
// Update zoomed view
if (ImageLoaded)
{
using (Graphics gfx = Graphics.FromImage(zoomBMP))
using (Bitmap CurrentImage_BMP = new Bitmap(currentImage))
{
// Fill background
using (Brush BGbrush = new SolidBrush(ObjectBG))
{
gfx.FillRectangle(BGbrush, 0, 0, zoomBMP.Width, zoomBMP.Height);
}
for (int x = 0; x < 41; x++)
{
int dx = x - 20;
int imageX = e.Location.X + dx;
// Check if x is within image
if ((imageX >= 0) && (imageX < currentImage.Width))
{
for (int y = 0; y < 41; y++)
{
int dy = y - 20;
int imageY = e.Location.Y + dy;
if ((imageY >= 0) && (imageY < currentImage.Height))
{
using (Brush pixelBrush = new SolidBrush(CurrentImage_BMP.GetPixel(imageX, imageY)))
{
gfx.FillRectangle(pixelBrush, 5 * x, 5 * y, 5, 5);
}
}
}
}
}
// Add crosshair
Rectangle[] OuterCrosshairRectangles =
{
// Top rect
new Rectangle(101, 0, 3, 99),
// Bottom rect
new Rectangle(101, 106, 3, 99),
// Left rect
new Rectangle(0, 101, 99, 3),
// Right rect
new Rectangle(106, 101, 99, 3)
};
using (Brush OuterCrosshairBrush = new SolidBrush(Color.Black))
{
gfx.FillRectangles(OuterCrosshairBrush, OuterCrosshairRectangles);
}
using (Pen InnerCrosshairPen = new Pen(Color.White))
{
// Top line
gfx.DrawLine(InnerCrosshairPen, 102.0f, 0.0f, 102.0f, 97.0f);
// Bottom line
gfx.DrawLine(InnerCrosshairPen, 102.0f, 107.0f, 102.0f, 204.0f);
// Left line
gfx.DrawLine(InnerCrosshairPen, 0.0f, 102.0f, 97.0f, 102.0f);
// Right line
gfx.DrawLine(InnerCrosshairPen, 107.0f, 102.0f, 204.0f, 102.0f);
}
}
// Update AlignImageView control
AlignImageView.Image = zoomBMP;
}
// Update coordinate label
switch (CurrentCoordinateState)
{
case CoordinateState.Coordinates:
CoordinateLabel.ForeColor = SystemColors.Control;
CoordinateLabel.Text = "X: " + e.Location.X.ToString() + " / Y: " + e.Location.Y.ToString();
break;
case CoordinateState.CalStart:
CoordinateLabel.ForeColor = SystemColors.Control;
CoordinateLabel.Text = "Select Edge Location with lowest Value | X: " + e.Location.X.ToString() + xAxisUnitBox.Text + " / Y: " + e.Location.Y.ToString() + yAxisUnitBox.Text;
break;
case CoordinateState.CalEnd:
CoordinateLabel.ForeColor = SystemColors.Control;
CoordinateLabel.Text = "Select Edge Location with highest Value | X: " + e.Location.X.ToString() + xAxisUnitBox.Text + " / Y: " + e.Location.Y.ToString() + yAxisUnitBox.Text;
break;
case CoordinateState.Units:
if (AllAxisParametersValid)
{
try
{
if (IsPointInGraphArea(e.Location))
{
Point GraphRelativeCursorLocation = PointToGraph(e.Location);
// Calculate X data
double xValue = 0.0;
double xScale = (double)(GraphRelativeCursorLocation.X) / (double)GraphWidth;
if (xAxisLogarithmic.Checked)
{
// X axis is logarithmic
double xAxisStartValue = Math.Log10(double.Parse(xAxisStartValueBox.Text));
double xAxisEndValue = Math.Log10(double.Parse(xAxisEndValueBox.Text));
xValue = Math.Pow(10, xAxisStartValue + xScale * (xAxisEndValue - xAxisStartValue));
}
else
{
// X axis is linear
double xAxisStartValue = double.Parse(xAxisStartValueBox.Text);
double xAxisEndValue = double.Parse(xAxisEndValueBox.Text);
xValue = xScale * (xAxisEndValue - xAxisStartValue) + xAxisStartValue;
}
// Calculate Y data
double yValue = 0.0;
double yScale = (double)(GraphRelativeCursorLocation.Y) / (double)GraphHeight;
if (yAxisLogarithmic.Checked)
{
// Y axis is logarithmic
double yAxisStartValue = Math.Log10(double.Parse(yAxisStartValueBox.Text));
double yAxisEndValue = Math.Log10(double.Parse(yAxisEndValueBox.Text));
yValue = Math.Pow(10, yAxisStartValue + yScale * (yAxisEndValue - yAxisStartValue));
}
else
{
// Y axis is linear
double yAxisStartValue = double.Parse(yAxisStartValueBox.Text);
double yAxisEndValue = double.Parse(yAxisEndValueBox.Text);
yValue = yScale * (yAxisEndValue - yAxisStartValue) + yAxisStartValue;
}
// Update coordinate label
CoordinateLabel.ForeColor = SystemColors.Control;
CoordinateLabel.Text = "X: " + RoundToSignificantDigits(xValue, 3).ToString("G15") + xAxisUnitBox.Text + " / Y: " + RoundToSignificantDigits(yValue, 3).ToString("G15") + yAxisUnitBox.Text;
}
else
{
CoordinateLabel.ForeColor = SystemColors.Control;
CoordinateLabel.Text = "Outside of graph area!";
}
}
catch
{
CoordinateLabel.ForeColor = Color.Red;
CoordinateLabel.Text = "Calculation falied!";
}
}
else
{
// Axis parameters missing, update label
CoordinateLabel.ForeColor = Color.Red;
CoordinateLabel.Text = "Some axis parameters are missing!";
}
break;
}
}
private void mainPictureBox_Click(object sender, EventArgs e)
{
switch (CurrentCoordinateState)
{
case CoordinateState.CalStart:
// Get the current mouse position relative to the picturebox
StartEdge = mainPictureBox.PointToClient(Cursor.Position);
// Update the current coordinate state
CurrentCoordinateState = CoordinateState.CalEnd;
break;
case CoordinateState.CalEnd:
// Get the current mouse position relative to the picturebox
EndEdge = mainPictureBox.PointToClient(Cursor.Position);
// Update the current coordinate state
CurrentCoordinateState = CoordinateState.Units;
break;
}
}
}
}