-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionFrame.cs
163 lines (143 loc) · 5.36 KB
/
SelectionFrame.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.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace PrintScreen
{
public class SelectionFrame : Form
{
private readonly Pen pen = new Pen(Color.Blue, 2.5f);
public SelectionFrame()
{
this.AutoScaleMode = AutoScaleMode.None;
this.FormBorderStyle = FormBorderStyle.None;
this.ShowInTaskbar = false;
this.TopLevel = true;
this.Cursor = Cursors.Cross;
}
private Bitmap captured;
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
if (Visible)
{
startPoint = null;
if (FormBorderStyle == FormBorderStyle.None)
{
Bounds = Rectangle.FromLTRB(
Screen.AllScreens.Min(s => s.Bounds.Left),
Screen.AllScreens.Min(s => s.Bounds.Top),
Screen.AllScreens.Max(s => s.Bounds.Right),
Screen.AllScreens.Max(s => s.Bounds.Bottom));
}
captured = new Bitmap(ClientSize.Width, ClientSize.Height);
using (var g = Graphics.FromImage(captured))
g.CopyFromScreen(PointToScreen(Point.Empty), Point.Empty, ClientSize);
TopMost = true;
}
else
{
captured?.Dispose();
captured = null;
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Escape) Hide();
}
private Point? startPoint;
private Point lastPosition;
protected override void OnMouseClick(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (startPoint == null)
{
startPoint = e.Location;
lastPosition = e.Location;
}
else
{
TakeScreenshot(GetRect(startPoint.Value, e.Location));
Hide();
}
}
else if (e.Button == MouseButtons.Right)
{
if (startPoint != null)
{
startPoint = null;
Invalidate();
}
else Hide();
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (startPoint == null) return;
using (var g = CreateGraphics())
{
foreach (var rect in GetDifferecne(startPoint.Value, e.Location, lastPosition))
{
rect.Inflate((int)Math.Ceiling(pen.Width), (int)Math.Ceiling(pen.Width));
g.DrawImage(captured, rect, rect, GraphicsUnit.Pixel);
}
g.DrawRectangle(pen, GetRect(startPoint.Value, e.Location));
lastPosition = e.Location;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (startPoint == null) return;
e.Graphics.DrawRectangle(pen, GetRect(startPoint.Value, lastPosition));
}
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.DrawImage(captured, e.ClipRectangle, e.ClipRectangle, GraphicsUnit.Pixel);
}
private static Rectangle GetRect(Point p1, Point p2)
{
return GetRect(p1.X, p1.Y, p2.X, p2.Y);
}
private static Rectangle GetRect(int x1, int y1, int x2, int y2)
{
return Rectangle.FromLTRB(Math.Min(x1, x2), Math.Min(y1, y2), Math.Max(x1, x2), Math.Max(y1, y2));
}
private static Rectangle GetRect(Point a, Point b, Point c)
{
return Rectangle.FromLTRB(
Math.Min(Math.Min(a.X, b.X), c.X),
Math.Min(Math.Min(a.Y, b.Y), c.Y),
Math.Max(Math.Max(a.X, b.X), c.X),
Math.Max(Math.Max(a.Y, b.Y), c.Y));
}
private static IEnumerable<Rectangle> GetDifferecne(Point s, Point a, Point b)
{
var bounds = GetRect(s, a, b);
yield return Rectangle.FromLTRB(bounds.Left, Math.Min(a.Y, b.Y), bounds.Right, Math.Max(a.Y, b.Y));
yield return Rectangle.FromLTRB(Math.Min(a.X, b.X), bounds.Top, Math.Max(a.X, b.X), bounds.Bottom);
}
private void TakeScreenshot(Rectangle rectangle)
{
using (var bitmap = new Bitmap(rectangle.Width, rectangle.Height))
{
using (var g = Graphics.FromImage(bitmap))
g.DrawImage(captured, 0, 0, rectangle, GraphicsUnit.Pixel);
var filename = $"{DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss")}.png";
var filepath = Path.Combine(ConfigurationManager.AppSettings["dropFilesPath"], filename);
bitmap.Save(filepath, ImageFormat.Png);
Clipboard.SetFileDropList(new StringCollection { filepath });
}
}
}
}