-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
158 lines (130 loc) · 5.69 KB
/
Form1.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
using System.Diagnostics;
using System.Drawing.Drawing2D;
namespace ImageComparison
{
public partial class Form1 : Form
{
Bitmap canvas = new Bitmap(1, 1);
//Bitmap img1 = new Bitmap("C:\\Users\\Nevenit\\Pictures\\kirito.png");
//Bitmap img2 = new Bitmap("C:\\Users\\Nevenit\\Pictures\\kirito3.png");
//Temprorary way to chose images
Bitmap img2 = new Bitmap("C:\\Users\\Nevenit\\Pictures\\SteamScreenshots\\2160p.png");
Bitmap img1 = new Bitmap("C:\\Users\\Nevenit\\Pictures\\SteamScreenshots\\1080p.png");
//Camera variables
float zoomValue = 1.0f;
float[] zoomPoint = new float[2] { 500, 500 };
//Mouse variables
bool lMouseDown = false;
bool rMouseDown = false;
bool mMouseDown = false;
Stopwatch stopwatch = new Stopwatch();
public Form1()
{
InitializeComponent();
}
private void MouseWheelEvent(object sender, MouseEventArgs e)
{
int numberOfTextLinesToMove = e.Delta * SystemInformation.MouseWheelScrollLines / 120;
int numberOfPixelsToMove = numberOfTextLinesToMove * 2;
int scale = numberOfPixelsToMove / 6;
zoomValue += (zoomValue / 20) * scale;
}
private void MouseDownEvent(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
lMouseDown = true;
break;
case MouseButtons.Right:
rMouseDown = true;
break;
case MouseButtons.Middle:
mMouseDown = true;
break;
case MouseButtons.None:
default:
break;
}
}
private void MouseUpEvent(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
lMouseDown = false;
break;
case MouseButtons.Right:
rMouseDown = false;
break;
case MouseButtons.Middle:
mMouseDown = false;
break;
case MouseButtons.None:
default:
break;
}
}
bool plMouseDown = false;
Point pPos = new Point(0,0);
private void mainLoop_Tick(object sender, EventArgs e)
{
stopwatch.Restart();
var timeStart = stopwatch.ElapsedMilliseconds;
// Update the canvas size for when the window is resized
int[] canvasSize = { pictureBox1.Width, pictureBox1.Height };
var pos = this.PointToClient(Cursor.Position);
// Dispose of the last canvas
canvas.Dispose();
canvas = new Bitmap(canvasSize[0], canvasSize[1]);
// Calculate image pos
int[] imgPos = { (int)((canvasSize[0] / 2) - (zoomPoint[0] * zoomValue)),
(int)((canvasSize[1] / 2) - (zoomPoint[1] * zoomValue))};
// Calculate image size
int[] imgSize = {
(int)(Math.Max(img1.Width, img2.Width) * zoomValue),
(int)(Math.Max(img1.Height, img2.Height) * zoomValue) };
// Make sure the mouse isnt beyond the image
int[] mousePos = { Math.Clamp(pos.X, imgPos[0], imgPos[0] + imgSize[0] + 1),
Math.Clamp(pos.Y, imgPos[1], imgPos[1] + imgSize[1])};
// Move camera
if (lMouseDown)
{
zoomPoint[0] -= (pos.X - pPos.X) / zoomValue;
zoomPoint[1] -= (pos.Y - pPos.Y) / zoomValue;
}
using (Graphics g = Graphics.FromImage(canvas))
{
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.PixelOffsetMode = PixelOffsetMode.Half;
g.CompositingQuality = CompositingQuality.HighSpeed;
// Mouse x relative to the image
int mouseXOnImg = mousePos[0] - imgPos[0];
// Large scaled image to small original image
int scaledImageWidth = (int)(mouseXOnImg / zoomValue);
// Width to nearest pixel
float flooredWidth = (float)(zoomValue * scaledImageWidth);
//label1.Text = String.Format("X: {0} \nY: {1}\nW: {2}\nMoI: {3}\nMoIBound: {4}\nBORKED?: {5}\nPixelSize: {6}\nFlooredWidth: {7}\nZoomValue: {8}", imgPos[0], imgPos[1], scaledImageWidth, mouseXOnImg, (mouseXOnImg % pixelSize), mouseXOnImg - (mouseXOnImg % pixelSize), pixelSize, flooredWidth, zoomValue);
using (Bitmap croppedImg = ImageProcessing.CropImage(img1, new Rectangle(img1.Width - scaledImageWidth, 0, 0, img1.Height)))
g.DrawImage(croppedImg, flooredWidth, imgPos[1], imgSize[0] - flooredWidth, imgSize[1]);
if (scaledImageWidth > 0)
using (Bitmap croppedImg = ImageProcessing.CropImage(img2, new Rectangle(0, 0, scaledImageWidth, img2.Height)))
g.DrawImage(croppedImg, imgPos[0], imgPos[1], flooredWidth, imgSize[1]);
Pen pen = new Pen(Color.FromArgb(255, 255, 255));
g.DrawLine(pen, pos.X, 0, pos.X, pictureBox1.Height);
}
var timeEnd = stopwatch.ElapsedMilliseconds;
label1.Text = timeEnd - timeStart + "ms";
pictureBox1.Image = canvas;
plMouseDown = lMouseDown;
pPos = pos;
}
}
}
/* TODO
*
* crop background image as well as the fron
*
*
*
*/