-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdesktop-window-screenshot-via-BitBlt.pas
More file actions
61 lines (49 loc) · 1.09 KB
/
desktop-window-screenshot-via-BitBlt.pas
File metadata and controls
61 lines (49 loc) · 1.09 KB
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
uses
System.SysUtils, Winapi.Windows, VCL.Graphics, VCL.Imaging.pngimage;
// ...
function DesktopScreenshot(ATargetWindow : THandle = 0) : TBitmap;
begin
result := nil;
///
if ATargetWindow = 0 then
ATargetWindow := GetDesktopWindow();
var ARect : TRect;
GetWindowRect(ATargetWindow, ARect);
if ARect.IsEmpty then
raise Exception.Create('Can''t capture a zero-bound window!');
result := TBitmap.Create();
result.Width := ARect.Width;
result.Height := ARect.Height;
var AWindowDC := GetWindowDC(ATargetWindow);
try
BitBlt(
result.Canvas.Handle,
0,
0,
result.Width,
result.Height,
AWindowDC,
0,
0,
SRCCOPY
);
finally
ReleaseDC(ATargetWindow, AWindowDC);
end;
end;
// ...
// Important to capture correct window bound on HDPI monitors.
SetProcessDPIAware();
///
var ABitmap := DesktopScreenshot();
if Assigned(ABitmap) then begin
var APng := TPngImage.Create();
try
APng.Assign(ABitmap);
///
APng.SaveToFile('screenshot.png');
finally
FreeAndNil(APng);
FreeAndNil(ABitmap);
end;
end;