-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageSelection.cs
231 lines (207 loc) · 8.37 KB
/
ImageSelection.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace TrayTools {
public partial class ImageSelection : Form {
Image image;
Rectangle selection;
State state;
Size size;
Point start;
Point end;
Pen line;
Rectangle top, left, right, bottom;
Brush shadow;
const int CORNER_RADIUS = 15;
Screen screen;
public ImageSelection(int index) {
InitializeComponent();
this.screen = Screen.AllScreens[index];
Rectangle bounds = new Rectangle();
foreach(Screen screen in Screen.AllScreens){
bounds.Width = Math.Max(bounds.Width, screen.Bounds.Width);
bounds.Height = Math.Max(bounds.Height, screen.Bounds.Height);
}
bounds.Location = new Point(bounds.Width * index, 0);
image = new Bitmap(bounds.Width, bounds.Height);
Graphics g = Graphics.FromImage(image);
g.CopyFromScreen(bounds.Location, new Point(0, 0), bounds.Size);
g.Dispose();
selection = Rectangle.Empty;
state = State.Idle;
// Drawing stuff
line = new Pen(new SolidBrush(Color.Red), 1);
shadow = new SolidBrush(Color.FromArgb(128, Color.Black));
}
void ImageSelection_Shown(object sender, EventArgs e) {
this.Bounds = this.screen.Bounds;
}
void ImageSelection_Paint(object sender, PaintEventArgs e) {
e.Graphics.DrawImage(image, 0, 0, this.Width, this.Height);
if (!selection.IsEmpty) {
Rectangle lineR = selection;
lineR.Width--;
lineR.Height--;
e.Graphics.DrawRectangle(line, lineR);
// Top
e.Graphics.FillRectangle(shadow, top);
// Bottom
e.Graphics.FillRectangle(shadow, bottom);
// Left
e.Graphics.FillRectangle(shadow, left);
// Right
e.Graphics.FillRectangle(shadow, right);
double factor = Math.Max(this.Width, this.Height) / 1920.0;
Font font = new Font("Arial", (int)(30 * factor), FontStyle.Bold, GraphicsUnit.Pixel);
Point pt = new Point(Math.Max((int)(font.Size * .30), selection.X + (int)(font.Size * .30)), Math.Max((int)(font.Size * .25), selection.Y - (int)(font.Size * 1.2)));
string text = string.Format("{0} x {1}", selection.Width, selection.Height);
e.Graphics.DrawString(text, font, new SolidBrush(Color.Red), pt);
} else {
Rectangle area = this.Bounds;
area.Location = Point.Empty;
e.Graphics.FillRectangle(shadow, area);
}
}
void ImageSelection_Scroll(object sender, ScrollEventArgs e) {
}
void ImageSelection_MouseDown(object sender, MouseEventArgs e) {
if (Distance2D(e.Location, new Point(selection.Right, selection.Bottom)) < CORNER_RADIUS) {
size = selection.Size;
end = e.Location;
state = State.Resizing;
} else {
if (selection.Contains(e.Location)) {
start = selection.Location;
end = e.Location;
state = State.Moving;
} else {
start = e.Location;
end = Point.Empty;
state = State.Selecting;
}
}
}
void ImageSelection_MouseUp(object sender, MouseEventArgs e) {
state = State.Idle;
}
void ImageSelection_MouseMove(object sender, MouseEventArgs e) {
switch (state) {
case State.Selecting: {
end = e.Location;
selection = Rectangle.FromLTRB(Math.Min(start.X, end.X), Math.Min(start.Y, end.Y), Math.Max(start.X, end.X), Math.Max(start.Y, end.Y));
UpdateSelection();
}
break;
case State.Moving: {
selection.X = Math.Min(Math.Max(0, start.X + (e.X - end.X)), this.Width - selection.Width);
selection.Y = Math.Min(Math.Max(0, start.Y + (e.Y - end.Y)), this.Height - selection.Height);
UpdateSelection();
}
break;
case State.Resizing: {
selection.Width = Math.Min(Math.Max(0, size.Width + (e.X - end.X)), this.Width - selection.X);
selection.Height = Math.Min(Math.Max(0, size.Height + (e.Y - end.Y)), this.Height - selection.Y);
UpdateSelection();
}
break;
default: {
if (Distance2D(e.Location, new Point(selection.Right, selection.Bottom)) < CORNER_RADIUS) {
this.Cursor = Cursors.SizeNWSE;
} else {
if (selection.Contains(e.Location)) {
this.Cursor = Cursors.SizeAll;
} else {
this.Cursor = Cursors.Default;
}
}
}
break;
}
}
void ImageSelection_KeyDown(object sender, KeyEventArgs e) {
switch (e.KeyData) {
case Keys.Left: {
selection.X--;
UpdateSelection();
}
break;
case Keys.Up: {
selection.Y--;
UpdateSelection();
}
break;
case Keys.Right: {
selection.X++;
UpdateSelection();
}
break;
case Keys.Down: {
selection.Y++;
UpdateSelection();
}
break;
case Keys.Escape:
case Keys.X: {
this.Close();
}
break;
case Keys.S: {
// Save
this.Hide();
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "bmp";
sfd.Filter = "Image (*.png)|*.png";
sfd.Title = "Save screenshot to...";
if (sfd.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) {
GetImage().Save(sfd.FileName);
}
this.Close();
}
break;
case Keys.C: {
// Clipboard
this.Close();
Clipboard.SetImage(GetImage());
}
break;
case Keys.V: {
// ImageViewer
ImageViewer imageViewer = new ImageViewer(GetImage(), image);
imageViewer.Show();
imageViewer.Focus();
this.Close();
}
break;
}
}
void UpdateSelection() {
top = new Rectangle(0, 0, this.Width, selection.Y);
bottom = new Rectangle(0, selection.Y + selection.Height, this.Width, this.Height - (selection.Y + selection.Height));
left = new Rectangle(0, selection.Y, selection.X, selection.Height);
right = new Rectangle(selection.X + selection.Width, selection.Y, this.Width - (selection.X + selection.Width), selection.Height);
this.Invalidate();
}
//public Image GetImage() {
public Bitmap GetImage() {
Bitmap bitmap = new Bitmap(selection.Width, selection.Height);
Graphics g = Graphics.FromImage(bitmap);
g.DrawImage(image, -selection.X, -selection.Y);
g.Dispose();
return bitmap;
}
public enum State {
Idle,
Selecting,
Moving,
Resizing,
}
public double Distance2D(Point p1, Point p2) {
return Math.Sqrt(Math.Pow((p2.X - p1.X), 2) + Math.Pow((p2.Y - p1.Y), 2));
}
}
}