This repository has been archived by the owner on May 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
SnippingTool.cs
166 lines (149 loc) · 6.03 KB
/
SnippingTool.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
//Licensed to the Apache Software Foundation(ASF) under one
//or more contributor license agreements.See the NOTICE file
//distributed with this work for additional information
//regarding copyright ownership.The ASF licenses this file
//to you under the Apache License, Version 2.0 (the
//"License"); you may not use this file except in compliance
//with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing,
//software distributed under the License is distributed on an
//"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//KIND, either express or implied. See the License for the
//specific language governing permissions and limitations
//under the License.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace Paragon.Plugins.ScreenCapture
{
/// <summary>
/// Utility for getting a screen-shot of a select portion of the screen.
/// </summary>
public sealed partial class SnippingTool : Form
{
public Rectangle SelectedRectangle = new Rectangle();
private Point _pntStart;
public SnippingTool(Image screenShot, Rectangle position)
{
InitializeComponent();
base.BackgroundImage = screenShot;
base.ShowInTaskbar = false;
base.FormBorderStyle = FormBorderStyle.None;
base.Left = position.Left;
base.Top = position.Top;
base.Size = position.Size;
base.DoubleBuffered = true;
StartPosition = FormStartPosition.Manual;
// if window loses focus for whatever reason, exit
Deactivate += OnSnippingToolDeactivate;
base.Load += OnLoad;
}
public Image Image { get; set; }
internal static InternalSnippet TakeSnippet()
{
var height = SystemInformation.VirtualScreen.Height;
var width = SystemInformation.VirtualScreen.Width;
var X = SystemInformation.VirtualScreen.X;
var Y = SystemInformation.VirtualScreen.Y;
var rectangle = new Rectangle(X, Y, width, height);
using (var bmp = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppPArgb))
{
using (var graphics = Graphics.FromImage(bmp))
{
using (var snipper = new SnippingTool(bmp, rectangle))
{
graphics.CopyFromScreen(X, Y, 0, 0, bmp.Size);
if (snipper.ShowDialog() == DialogResult.OK)
{
return new InternalSnippet(snipper.Image, snipper.SelectedRectangle);
}
return null;
}
}
}
}
private void OnLoad(object sender, EventArgs e)
{
base.Load -= OnLoad;
base.Cursor = Cursors.Cross;
}
private void OnSnippingToolDeactivate(object sender, EventArgs e)
{
base.DialogResult = DialogResult.Cancel;
}
protected override void OnMouseDown(MouseEventArgs e)
{
// Start the snip on mouse down
if (e.Button != MouseButtons.Left)
{
return;
}
_pntStart = e.Location;
SelectedRectangle = new Rectangle(e.Location, new Size(0, 0));
base.Invalidate();
}
protected override void OnMouseMove(MouseEventArgs e)
{
// Modify the selection on mouse move
if (e.Button != MouseButtons.Left)
{
return;
}
var x1 = Math.Min(e.X, _pntStart.X);
var y1 = Math.Min(e.Y, _pntStart.Y);
var x2 = Math.Max(e.X, _pntStart.X);
var y2 = Math.Max(e.Y, _pntStart.Y);
SelectedRectangle = new Rectangle(x1, y1, x2 - x1, y2 - y1);
base.Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
// Complete the snip on mouse-up
if (SelectedRectangle.Width <= 0 || SelectedRectangle.Height <= 0)
{
return;
}
// unbind so that the correct result is returned
Deactivate -= OnSnippingToolDeactivate;
Image = new Bitmap(SelectedRectangle.Width, SelectedRectangle.Height);
using (var gr = Graphics.FromImage(Image))
{
gr.DrawImage(base.BackgroundImage, new Rectangle(0, 0, Image.Width, Image.Height),
SelectedRectangle, 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)))
{
var x1 = SelectedRectangle.X;
var x2 = SelectedRectangle.X + SelectedRectangle.Width;
var y1 = SelectedRectangle.Y;
var y2 = SelectedRectangle.Y + SelectedRectangle.Height;
e.Graphics.FillRectangle(br, new Rectangle(0, 0, x1, base.Height));
e.Graphics.FillRectangle(br, new Rectangle(x2, 0, base.Width - x2, base.Height));
e.Graphics.FillRectangle(br, new Rectangle(x1, 0, x2 - x1, y1));
e.Graphics.FillRectangle(br, new Rectangle(x1, y2, x2 - x1, base.Height - y2));
}
using (var pen = new Pen(Color.Red, 3))
{
e.Graphics.DrawRectangle(pen, SelectedRectangle);
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// Allow canceling the snip with the Escape key
if (keyData == Keys.Escape)
{
base.DialogResult = DialogResult.Cancel;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
}