-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProPictureBox.cs
163 lines (141 loc) · 4.27 KB
/
ProPictureBox.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
using System;
using System.Drawing;
using System.Windows.Forms;
namespace FindCircles
{
public struct ProTransformation
{
public Point Translation { get { return _translation; } }
public double Scale { get { return _scale; } }
private readonly Point _translation;
private readonly double _scale;
public ProTransformation(Point translation, double scale)
{
_translation = translation;
_scale = scale;
}
public Point ConvertToIm(Point p)
{
return new Point((int)(p.X * _scale + _translation.X), (int)(p.Y * _scale + _translation.Y));
}
public Size ConvertToIm(Size p)
{
return new Size((int)(p.Width * _scale), (int)(p.Height * _scale));
}
public Rectangle ConvertToIm(Rectangle r)
{
return new Rectangle(ConvertToIm(r.Location), ConvertToIm(r.Size));
}
public Point ConvertToPb(Point p)
{
return new Point((int)((p.X - _translation.X) / _scale), (int)((p.Y - _translation.Y) / _scale));
}
public Size ConvertToPb(Size p)
{
return new Size((int)(p.Width / _scale), (int)(p.Height / _scale));
}
public Rectangle ConvertToPb(Rectangle r)
{
return new Rectangle(ConvertToPb(r.Location), ConvertToPb(r.Size));
}
public ProTransformation SetTranslate(Point p)
{
return new ProTransformation(p, _scale);
}
public ProTransformation AddTranslate(Point p)
{
return SetTranslate(new Point(p.X + _translation.X, p.Y + _translation.Y));
}
public ProTransformation SetScale(double scale)
{
return new ProTransformation(_translation, scale);
}
}
public class ProPictureBox : PictureBox
{
private Point? _clickedPoint;
private ProTransformation _transformation;
public ProTransformation Transformation
{
set
{
_transformation = FixTranslation(value);
Invalidate();
}
get
{
return _transformation;
}
}
public ProPictureBox()
{
_transformation = new ProTransformation(new Point(100, 0), .5f);
MouseDown += OnMouseDown;
MouseMove += OnMouseMove;
MouseUp += OnMouseUp;
MouseWheel += OnMouseWheel;
Resize += OnResize;
}
private ProTransformation FixTranslation(ProTransformation value)
{
var maxScale = Math.Max((double)Image.Width / ClientRectangle.Width, (double)Image.Height / ClientRectangle.Height);
if (value.Scale > maxScale)
value = value.SetScale(maxScale);
if (value.Scale < 0.3)
value = value.SetScale(0.3);
var rectSize = value.ConvertToIm(ClientRectangle.Size);
var max = new Size(Image.Width - rectSize.Width, Image.Height - rectSize.Height);
value = value.SetTranslate((new Point(Math.Min(value.Translation.X, max.Width), Math.Min(value.Translation.Y, max.Height))));
if (value.Translation.X < 0 || value.Translation.Y < 0)
{
value = value.SetTranslate(new Point(Math.Max(value.Translation.X, 0), Math.Max(value.Translation.Y, 0)));
}
return value;
}
private void OnResize(object sender, EventArgs eventArgs)
{
if (Image == null)
return;
Transformation = Transformation;
}
private void OnMouseWheel(object sender, MouseEventArgs e)
{
var transformation = _transformation;
var pos1 = transformation.ConvertToIm(e.Location);
if (e.Delta > 0)
transformation = (transformation.SetScale(Transformation.Scale / 1.25));
else
transformation = (transformation.SetScale(Transformation.Scale * 1.25));
var pos2 = transformation.ConvertToIm(e.Location);
transformation = transformation.AddTranslate(pos1 - (Size)pos2);
Transformation = transformation;
}
private void OnMouseUp(object sender, MouseEventArgs mouseEventArgs)
{
_clickedPoint = null;
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (_clickedPoint == null)
return;
var p = _transformation.ConvertToIm((Size)e.Location);
Transformation = _transformation.SetTranslate(_clickedPoint.Value - p);
}
private void OnMouseDown(object sender, MouseEventArgs e)
{
Focus();
_clickedPoint = _transformation.ConvertToIm(e.Location);
}
protected override void OnPaint(PaintEventArgs e)
{
var imRect = Transformation.ConvertToIm(ClientRectangle);
if (Image == null)
return;
e.Graphics.DrawImage(Image, ClientRectangle, imRect, GraphicsUnit.Pixel);
}
public void DecideInitialTransformation()
{
Transformation = new ProTransformation(Point.Empty, int.MaxValue);
}
}
}