-
Notifications
You must be signed in to change notification settings - Fork 23
/
screengrabber.pas
246 lines (208 loc) · 5.99 KB
/
screengrabber.pas
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
238
239
240
241
242
243
244
245
246
unit ScreenGrabber;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, ZStream { for Tcompressionlevel };
type
TImageFormat = (fmtPNG=0, fmtJPG, fmtBMP{, fmtGIF}, fmtTIFF);
TColorDepth = (cd8Bit=8, cd16Bit=16, cd24Bit=24, cd32Bit=32);
TImageFormatInfo = record
Name: String[10];
Extension: String[3];
HasQuality: Boolean;
HasGrayscale: Boolean;
ColorDepth: Set of TColorDepth;
HasCompressionLevel: Boolean;
end;
TImageFormatInfoArray = array [TImageFormat] of TImageFormatInfo;
{ TScreenGrabber }
TScreenGrabber = class
private
procedure CaptureRegion(AFileName: String; ARect: TRect);
public
ImageFormat: TImageFormat;
ColorDepth: TColorDepth;
Quality: Integer;
IsGrayscale: Boolean;
CompressionLevel: Tcompressionlevel;
constructor Create(AnImageFormat: TImageFormat; AColorDepth: TColorDepth;
AnQuality: Integer; AnIsGrayscale: Boolean; ACompressionLevel: Tcompressionlevel);
procedure CaptureMonitor(AFileName: String; AMonitorId: Integer);
procedure CaptureAllMonitors(AFileName: String);
end;
const
ImageFormatInfoArray: TImageFormatInfoArray = (
(
Name: 'PNG';
Extension: 'png';
HasQuality: False;
HasGrayscale: True;
ColorDepth: [{cd8Bit, cd16Bit, cd24Bit, cd32Bit}];
HasCompressionLevel: True
),
(
Name: 'JPG';
Extension: 'jpg';
HasQuality: True;
HasGrayscale: True;
ColorDepth: [];
HasCompressionLevel: False
),
(
Name: 'BMP';
Extension: 'bmp';
HasQuality: False;
HasGrayscale: False;
ColorDepth: [{cd8Bit,} cd16Bit, cd24Bit, cd32Bit];
HasCompressionLevel: False
){,
(
Name: 'GIF';
Extension: 'gif';
HasQuality: False;
HasGrayscale: False;
ColorDepth: [];
HasCompressionLevel: False
)},
(
Name: 'TIFF';
Extension: 'tif';
HasQuality: False;
HasGrayscale: False;
ColorDepth: [];
HasCompressionLevel: False
)
);
implementation
uses
{$IfDef Windows}
windows,
{$EndIf}
Forms {for TMonitor}, LCLType, LCLIntf, BGRABitmap, BGRABitmapTypes, FPWriteJPEG, FPWriteBMP,
FPWritePNG, FPImage, FPWriteTiff, LazLoggerBase;
{ TScreenGrabber }
procedure TScreenGrabber.CaptureMonitor(AFileName: String; AMonitorId: Integer);
var
Rect: TRect;
UsedMonitor: TMonitor;
begin
DebugLn(['Monitor id=', AMonitorId]);
UsedMonitor := Screen.Monitors[AMonitorId];
Rect.Left := UsedMonitor.Left;
Rect.Top := UsedMonitor.Top;
Rect.Width := UsedMonitor.Width;
Rect.Height := UsedMonitor.Height;
CaptureRegion(AFileName, Rect);
end;
procedure TScreenGrabber.CaptureAllMonitors(AFileName: String);
var
Rect: TRect;
begin
Rect.Left := GetSystemMetrics(SM_XVIRTUALSCREEN);
Rect.Top := GetSystemMetrics(SM_YVIRTUALSCREEN);
Rect.Width := GetSystemMetrics(SM_CXVIRTUALSCREEN);
Rect.Height := GetSystemMetrics(SM_CYVIRTUALSCREEN);
CaptureRegion(AFileName, Rect);
end;
procedure TScreenGrabber.CaptureRegion(AFileName: String; ARect: TRect);
{$IfDef Linux}
const
HWND_DESKTOP = 0;
{$EndIf}
var
Bitmap: TBGRABitmap;
Writer: TFPCustomImageWriter;
//GIF: TGIFImage;
ScreenDC: {$IfDef Windows}Windows.{$EndIf}HDC;
begin
DebugLn('Start taking screenshot...');
DebugLn('Region: ', DbgS(ARect));
Bitmap := TBGRABitmap.Create(ARect.Width, ARect.Height, BGRABlack);
//Bitmap.TakeScreenshot(Rect); // Not supports multiply monitors
ScreenDC := GetDC(HWND_DESKTOP); // Get DC for all monitors
DebugLn('ScreenDC=', DbgS(ScreenDC));
{$IfDef Windows}
// https://github.com/artem78/AutoScreenshot/issues/35
// and https://github.com/bgrabitmap/bgrabitmap/issues/200
BitBlt(Bitmap.Canvas.Handle, 0, 0, ARect.Width, ARect.Height,
ScreenDC, ARect.Left, ARect.Top, SRCCOPY);
{$EndIf}
{$IfDef Linux}
// ToDo: Check bug #35 in Linux
Bitmap.LoadFromDevice(ScreenDC, ARect);
{$EndIf}
ReleaseDC(0, ScreenDC);
case ImageFormat of
fmtPNG: // PNG
begin
Writer := TFPWriterPNG.create;
with Writer as TFPWriterPNG do
begin
GrayScale := IsGrayscale;
CompressionLevel := Self.CompressionLevel;
//Indexed := ...;
//UseAlpha := ...;
end;
end;
fmtJPG: // JPEG
begin
Writer := TFPWriterJPEG.Create;
with Writer as TFPWriterJPEG do
begin
CompressionQuality := Quality;
GrayScale := IsGrayscale;
end;
end;
fmtBMP: // Bitmap (BMP)
begin
Writer := TFPWriterBMP.Create;
with Writer as TFPWriterBMP do
begin
BitsPerPixel := Integer(ColorDepth);
//RLECompress := ...;
end;
end;
{fmtGIF: // GIF
begin
GIF := TGIFImage.Create;
try
GIF.Assign(Bitmap);
//GIF.OptimizeColorMap;
GIF.SaveToFile(AFileName);
finally
GIF.Free;
end;
end;}
fmtTIFF:
begin
Writer := TFPWriterTiff.Create;
end;
end;
try
try
Bitmap.SaveToFile(AFileName, Writer);
except
on E : Exception do
begin
DebugLn('Failed to take screenshot: ', E.ToString);
raise e;
end;
end;
finally
Writer.Free;
Bitmap.Free;
end;
DebugLn('Screenshot saved to ', AFileName);
end;
constructor TScreenGrabber.Create(AnImageFormat: TImageFormat;
AColorDepth: TColorDepth; AnQuality: Integer; AnIsGrayscale: Boolean;
ACompressionLevel: Tcompressionlevel);
begin
inherited Create();
ImageFormat := AnImageFormat;
ColorDepth := AColorDepth;
Quality := AnQuality;
IsGrayscale := AnIsGrayscale;
CompressionLevel := ACompressionLevel;
end;
end.