-
Notifications
You must be signed in to change notification settings - Fork 1
/
CaptureHelper.cs
237 lines (208 loc) · 5.57 KB
/
CaptureHelper.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
232
233
234
235
236
237
using Gma.UserActivityMonitor;
using PaJaMa.Common;
using PaJaMa.WinControls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PaJaMa.ScreenCapture
{
public class CaptureHelper : IDisposable
{
private List<frmOverlay> _overlays = new List<frmOverlay>();
private int? _downX = null;
private int? _downY = null;
private GlobalHooks _hooks;
private frmCaptureVideo _frmCaptureVideo;
public event CaptureEventHandler Captured;
public KeyPressEventHandler KeyPress;
public bool CaptureVideo { get; set; }
public CaptureHelper()
{
_hooks = new GlobalHooks();
_hooks.KeyDown += _hooks_KeyDown;
_hooks.KeyPress += _hooks_KeyPress;
}
private void _hooks_KeyPress(object sender, KeyPressEventArgs e)
{
if (KeyPress != null)
{
KeyPress(this, e);
}
}
private void _hooks_MouseMove(object sender, MouseEventArgs e)
{
if (_overlays.Any())
{
bool mouseDown = _downX >= 0 && _downY >= 0;
foreach (var overlay in _overlays)
{
if (overlay.IsInControl(Cursor.Position.X, Cursor.Position.Y) || mouseDown)
{
overlay.Draw(Cursor.Position.X, Cursor.Position.Y, _downX, _downY, mouseDown);
}
else
overlay.ClearGraphics();
}
}
}
private void _hooks_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.PrintScreen)
{
if (e.Control)
{
var activeScreen = Screen.FromPoint(Cursor.Position);
CaptureScreen(activeScreen, true);
}
else
StartCapture();
}
else if (e.KeyCode == Keys.Escape && _overlays.Any())
{
closeAll();
detachHooks();
_downX = null;
_downY = null;
}
}
private void _hooks_LeftMouseUp(object sender, MouseEventArgs e)
{
if (_overlays.Any())
{
foreach (var overlay in _overlays)
{
if (overlay.IsInControl(Cursor.Position.X, Cursor.Position.Y))
{
if (this.CaptureVideo)
{
_frmCaptureVideo = new frmCaptureVideo();
_frmCaptureVideo.SetCaptureBounds(overlay.GetRectangle());
_frmCaptureVideo.VideoCaptured += delegate (object s2, EventArgs e2)
{
Captured(this, new CaptureEventArgs() { Images = _frmCaptureVideo.CapturedImages });
_frmCaptureVideo.Dispose();
_frmCaptureVideo = null;
};
_frmCaptureVideo.Show();
break;
}
else
{
using (var image = overlay.GetImage())
{
Captured(this, new CaptureEventArgs() { Images = new List<Image>() { image } });
break;
}
}
}
}
closeAll();
detachHooks();
}
_downX = null;
_downY = null;
}
private void _hooks_LeftMouseDown(object sender, MouseEventArgs e)
{
_downX = Cursor.Position.X;
_downY = Cursor.Position.Y;
var args = (CancelMouseEventArgs)e;
args.Cancel = true;
}
private void detachHooks()
{
_hooks.MouseMove -= _hooks_MouseMove;
_hooks.LeftMouseDown -= _hooks_LeftMouseDown;
_hooks.LeftMouseUp -= _hooks_LeftMouseUp;
_hooks.DeatchMouseHook();
}
private void closeAll()
{
foreach (var overlay in _overlays)
{
overlay.Close();
overlay.Dispose();
}
_overlays.Clear();
Cursor.Show();
}
public void StartCapture()
{
Cursor.Hide();
_hooks.AttachMouseHook();
_hooks.MouseMove += _hooks_MouseMove;
_hooks.LeftMouseDown += _hooks_LeftMouseDown;
_hooks.LeftMouseUp += _hooks_LeftMouseUp;
foreach (var screen in Screen.AllScreens)
{
var overlay = new frmOverlay();
overlay.SetBounds(screen.WorkingArea.X, screen.WorkingArea.Y, screen.WorkingArea.Width, screen.WorkingArea.Height);
overlay.ScreenX = screen.WorkingArea.X;
overlay.ScreenY = screen.WorkingArea.Y;
overlay.StartPosition = FormStartPosition.Manual;
overlay.Show();
_overlays.Add(overlay);
}
}
public void CaptureFullScreen()
{
int xStop = 0;
int yStop = 0;
foreach (var screen in Screen.AllScreens)
{
if (screen.Bounds.Right > xStop)
xStop = screen.Bounds.Right;
if (screen.Bounds.Bottom > yStop)
yStop = screen.Bounds.Bottom;
}
using (var bmp = new Bitmap(xStop, yStop))
{
using (var graphics = Graphics.FromImage(bmp))
{
graphics.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(xStop, yStop));
}
Captured(this, new CaptureEventArgs() { Images = new List<Image>() { bmp } });
}
}
public void CaptureScreen(Screen screen, bool suspendShow = false)
{
using (var bmp = new Bitmap(screen.Bounds.Width, screen.Bounds.Height))
{
using (var graphics = Graphics.FromImage(bmp))
{
graphics.CopyFromScreen(screen.Bounds.Location, new Point(0, 0), screen.Bounds.Size);
}
Captured(this, new CaptureEventArgs() { Images = new List<Image>() { bmp }, SuspendShow = suspendShow });
}
}
public void CaptureVideoScreen(Screen screen)
{
_frmCaptureVideo = new frmCaptureVideo();
_frmCaptureVideo.SetCaptureBounds(screen.Bounds);
_frmCaptureVideo.VideoCaptured += delegate (object s2, EventArgs e2)
{
Captured(this, new CaptureEventArgs() { Images = _frmCaptureVideo.CapturedImages });
_frmCaptureVideo.Dispose();
_frmCaptureVideo = null;
};
_frmCaptureVideo.Show();
}
public void Dispose()
{
_hooks.Dispose();
_hooks = null;
}
}
public delegate void CaptureEventHandler(object sender, CaptureEventArgs e);
public class CaptureEventArgs : EventArgs
{
public List<Image> Images { get; set; }
public bool SuspendShow { get; set; }
}
}