UC_PicZoom.cs
10.9 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace AutoCountMachine_Single
{
public partial class UC_PicZoom : UserControl
{
Image img;
Point mouseDown;
int startx = 0;
int starty = 0;
int imgx = 0;
int imgy = 0;
bool mousepressed = false;
float zoom = 1;
float basezoom = 1;
float dpiX = 0F;
bool _TemplateMode=false;
public bool TemplateMode
{
get
{
return _TemplateMode;
}
set
{
_TemplateMode = value;
pictureBox.Refresh();
}
}
public PictureBox GetPictureBox
{
get { return pictureBox; }
}
public UC_PicZoom()
{
InitializeComponent();
//string imagefilename = @"d:\resout\Img20201021150145-Mark.png";
//img = Image.FromFile(imagefilename);
Graphics g = this.CreateGraphics();
dpiX = g.DpiX;
pictureBox.Paint += new PaintEventHandler(imageBox_Paint);
pictureBox.MouseMove += pictureBox_MouseMove;
pictureBox.MouseDown += imageBox_MouseDown;
pictureBox.MouseUp += imageBox_MouseUp;
pictureBox.MouseWheel += PictureBox_MouseWheel;
pictureBox.MouseDoubleClick += PictureBox_MouseDoubleClick;
}
private void PictureBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (zoom / basezoom > 4F)
{
zoom = basezoom;
doZoom(e.Location, false, 0F);
}
else
doZoom(e.Location, true, 2.5F);
}
private void PictureBox_MouseWheel(object sender, MouseEventArgs e)
{
//OnMouseWheel(e);
}
int lockisa = 0;
private void pictureBox_MouseMove(object sender, EventArgs e)
{
MouseEventArgs mouse = e as MouseEventArgs;
bool inresize=false;
int isa = 0;
if (TemplateMode && lockisa>=0 && mouseontheline(mouse, out isa))
{
if (isa==1)
{
pictureBox.Cursor = Cursors.SizeWE;
}
else if (isa == 2)
{
pictureBox.Cursor = Cursors.SizeNS;
}
if (isa > 0) {
lockisa = isa;
inresize = true;
}
}
else
{
pictureBox.Cursor = Cursors.Default;
}
if (mouse.Button == MouseButtons.Left)
{
if (TemplateMode && lockisa == 0) {
lockisa = isa;
}
if (lockisa<=0)
{
lockisa = -1;
Point mousePosNow = mouse.Location;
int deltaX = mousePosNow.X - mouseDown.X;
int deltaY = mousePosNow.Y - mouseDown.Y;
imgx = (int)(startx + (deltaX / zoom));
imgy = (int)(starty + (deltaY / zoom));
}
else if (lockisa>0)
{
//rect.Width = (int)(mouse.X * zoom);
//rect.Height = (int)(mouse.Y * zoom);
if (lockisa==1)
rect.Width = mouse.X - rect.X;
else if (lockisa == 2)
rect.Height = mouse.Y - rect.Y;
if (rect.Width < 10)
rect.Width = 10;
if (rect.Height < 10)
rect.Height = 10;
}
pictureBox.Refresh();
}
else {
lockisa = 0;
}
//Console.WriteLine($"x:{mouse.X},y:{mouse.Y}");
//Console.WriteLine($"imgx:{imgx},imgy:{imgx}");
}
bool mouseontheline(MouseEventArgs mouse, out int isa) {
isa = 0;
int offset = 6;
if (mouse.X > rect.X+rect.Width - offset && mouse.X < rect.X + rect.Width + offset && mouse.Y> rect.Y && mouse.Y< rect.Y+rect.Height)
{
isa = 1;
return true;
}
if (mouse.Y > rect.Y + rect.Height - offset && mouse.Y < rect.Y + rect.Height + offset && mouse.X > rect.X && mouse.X < rect.X + rect.Width)
{
isa = 2;
return true;
}
return false;
}
private void imageBox_MouseDown(object sender, EventArgs e)
{
MouseEventArgs mouse = e as MouseEventArgs;
if (mouse.Button == MouseButtons.Left)
{
if (!mousepressed)
{
mousepressed = true;
mouseDown = mouse.Location;
startx = imgx;
starty = imgy;
}
}
}
private void imageBox_MouseUp(object sender, EventArgs e)
{
mousepressed = false;
}
protected override void OnMouseWheel(MouseEventArgs e)
{
MouseEventArgs mouse = e as MouseEventArgs;
if (e.Delta > 0)
{
doZoom(mouse.Location, true);
}
else if (e.Delta < 0)
{
doZoom(mouse.Location, false);
}
}
void doZoom(Point mousePoint, bool isZoomin, float zoonRate = 0.2F)
{
float oldzoom = zoom;
if (isZoomin)
{
//zoom += 0.1F;
zoom = zoom * (1F + zoonRate);
}
else
{
//zoom = Math.Max(zoom - 0.1F, basezoon);
zoom = Math.Max(zoom * (1F - zoonRate), basezoom);
}
if (_TemplateMode && zoom>4) {
zoom = 4;
}
Console.WriteLine(zoom);
Point mousePosNow = mousePoint;
//点击坐标
int x = mousePosNow.X - pictureBox.Location.X;
int y = mousePosNow.Y - pictureBox.Location.Y;
//对应现在的图像坐标
int oldimagex = (int)(x / oldzoom);
int oldimagey = (int)(y / oldzoom);
//对应放大后的图像坐标
int newimagex = (int)(x / zoom);
int newimagey = (int)(y / zoom);
imgx = newimagex - oldimagex + imgx;
imgy = newimagey - oldimagey + imgy;
pictureBox.Refresh();
}
public Rectangle GetRectangle
{
get {
return new Rectangle((int)(imgx * -1 + rect.X / zoom), (int)(imgy * -1 + rect.Y / zoom), (int)(rect.Width / zoom), (int)(rect.Height / zoom));
}
}
Rectangle rect = new Rectangle(150, 150, 150, 150);
private void imageBox_Paint(object sender, PaintEventArgs e)
{
if (img == null)
return;
if (zoom == basezoom)
{
imgx = 0;
imgy = 0;
}
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.ScaleTransform(zoom, zoom);
e.Graphics.DrawImage(img, imgx, imgy);
if (TemplateMode)
{
Brush brush = new SolidBrush(Color.Red);
Pen pen = new Pen(brush, 1f / zoom);
e.Graphics.DrawRectangle(pen, new Rectangle((int)(rect.X / zoom), (int)(rect.Y / zoom), (int)(rect.Width / zoom), (int)(rect.Height / zoom)));
Console.WriteLine("rect.Width / zoom=" + rect.Width / zoom);
}
}
public void rectAdd(int width, int height) {
rect.Width += (int)(width * zoom);
rect.Height += (int)(height * zoom);
pictureBox.Refresh();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
const int WM_KEYDOWN = 0x100;
const int WM_SYSKEYDOWN = 0x104;
if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
switch (keyData)
{
case Keys.Right:
imgx -= (int)(pictureBox.Width * 0.1F / zoom);
pictureBox.Refresh();
break;
case Keys.Left:
imgx += (int)(pictureBox.Width * 0.1F / zoom);
pictureBox.Refresh();
break;
case Keys.Down:
imgy -= (int)(pictureBox.Height * 0.1F / zoom);
pictureBox.Refresh();
break;
case Keys.Up:
imgy += (int)(pictureBox.Height * 0.1F / zoom);
pictureBox.Refresh();
break;
case Keys.PageDown:
imgy -= (int)(pictureBox.Height * 0.90F / zoom);
pictureBox.Refresh();
break;
case Keys.PageUp:
imgy += (int)(pictureBox.Height * 0.90F / zoom);
pictureBox.Refresh();
break;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
public Image Image {
set {
if (img != null)
img.Dispose();
img = value;
if (value == null) {
pictureBox.Enabled = false;
return;
}
pictureBox.Enabled = true;
zoom = ((float)pictureBox.Width / (float)img.Width) * (img.HorizontalResolution / dpiX);
basezoom = zoom;
if (pictureBox.Height > pictureBox.Width)
{
zoom = ((float)pictureBox.Width / (float)img.Width) * (img.HorizontalResolution / dpiX);
imgy = (int)((img.Height - pictureBox.Height) * zoom / -2);
}
else
{
zoom = ((float)pictureBox.Height / (float)img.Height) * (img.VerticalResolution / dpiX);
imgx = (int)((img.Width - pictureBox.Width)*zoom / -2);
}
rect.X = (int)((img.Width / 2 - 500 / 2)*zoom);
rect.Y = (int)((img.Height / 4 - 400 / 2)* zoom);
rect.Width = (int)(500 * zoom);
rect.Height = (int)(400 * zoom);
pictureBox.Refresh();
}
}
}
}