-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSnippingCat.cs
124 lines (117 loc) · 4.56 KB
/
SnippingCat.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
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace BusinessCats
{
public partial class SnippingTool : Form
{
public static Rectangle GetFullScreenRect()
{
Rectangle rc = new Rectangle();
foreach (var screen in Screen.AllScreens)
{
if (rc == (new Rectangle()))
{
rc = screen.Bounds;
}
else
{
rc = Rectangle.Union(rc, screen.Bounds);
}
}
return rc;
}
public static Image Snip()
{
var rc = GetFullScreenRect();
using (Bitmap bmp = new Bitmap(rc.Width, rc.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb))
{
using (Graphics gr = Graphics.FromImage(bmp))
gr.CopyFromScreen(rc.Left, rc.Top, 0, 0, bmp.Size);
using (var snipper = new SnippingTool(bmp))
{
if (snipper.ShowDialog() == DialogResult.OK)
{
return snipper.Image;
}
}
return null;
}
}
public SnippingTool(Image screenShot)
{
this.ShowInTaskbar = false;
this.FormBorderStyle = FormBorderStyle.None;
//this.WindowState = FormWindowState.Maximized;
this.DoubleBuffered = true;
this.StartPosition = FormStartPosition.Manual;
var rc = GetFullScreenRect();
this.Location = new Point(rc.Left, rc.Top);
this.Bounds = rc;
this.BackgroundImage = screenShot;
this.Cursor = Cursors.Cross;
}
public Image Image { get; set; }
private Rectangle rcSelect = new Rectangle();
private Point pntStart;
protected override void OnMouseDown(MouseEventArgs e)
{
// Start the snip on mouse down
if (e.Button != MouseButtons.Left) return;
pntStart = e.Location;
rcSelect = new Rectangle(e.Location, new Size(0, 0));
this.Invalidate();
}
protected override void OnMouseMove(MouseEventArgs e)
{
// Modify the selection on mouse move
if (e.Button != MouseButtons.Left) return;
Rectangle rcPrevious = rcSelect;
int x1 = Math.Min(e.X, pntStart.X);
int y1 = Math.Min(e.Y, pntStart.Y);
int x2 = Math.Max(e.X, pntStart.X);
int y2 = Math.Max(e.Y, pntStart.Y);
rcSelect = new Rectangle(x1, y1, x2 - x1, y2 - y1);
this.Cursor = Cursors.Cross;
var rcInvalid = Rectangle.Union(rcPrevious, rcSelect);
rcInvalid.Inflate(8, 8);
this.Invalidate(rcInvalid);
}
protected override void OnMouseUp(MouseEventArgs e)
{
// Complete the snip on mouse-up
if (rcSelect.Width <= 0 || rcSelect.Height <= 0) return;
Image = new Bitmap(rcSelect.Width, rcSelect.Height);
using (Graphics gr = Graphics.FromImage(Image))
{
gr.DrawImage(this.BackgroundImage, new Rectangle(0, 0, Image.Width, Image.Height),
rcSelect, GraphicsUnit.Pixel);
}
DialogResult = DialogResult.OK;
}
protected override void OnPaint(PaintEventArgs e)
{
// Draw the current selection
using (Brush br = new SolidBrush(Color.FromArgb(120, Color.White)))
{
int x1 = rcSelect.X; int x2 = rcSelect.X + rcSelect.Width;
int y1 = rcSelect.Y; int y2 = rcSelect.Y + rcSelect.Height;
e.Graphics.FillRectangle(br, new Rectangle(0, 0, x1, this.Height));
e.Graphics.FillRectangle(br, new Rectangle(x2, 0, this.Width - x2, this.Height));
e.Graphics.FillRectangle(br, new Rectangle(x1, 0, x2 - x1, y1));
e.Graphics.FillRectangle(br, new Rectangle(x1, y2, x2 - x1, this.Height - y2));
}
using (Pen pen = new Pen(Color.Red, 3))
{
e.Graphics.DrawRectangle(pen, rcSelect);
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// Allow canceling the snip with the Escape key
if (keyData == Keys.Escape) this.DialogResult = DialogResult.Cancel;
return base.ProcessCmdKey(ref msg, keyData);
}
}
}