RawImage_FromDevice fails with negative coordinates
- Lazarus/FPC Version: 2.2.4
- Operating System: Windows XP, 7 and 10
- CPU / Bitness: probably both
- BGRABitmap: 11.5.4
Note: this issue was copied from bgrabitmap issue #200. As such it requires BGRABitmap though it could be easily adapter to call RawImage_FromDevice
directly. I don't have myself multiple monitors so I can't check. I've notified the original author of the bugreport that a project depending only on LCL would be preferable.
What happens
When trying to get a screenshot of multiple monitors, RawImage_FromDevice
fails.
program ScreenshotFailsDemo;
var
Bitmap: TBGRABitmap;
ScreenDC: Windows.HDC;
Rect: TRect;
begin
Rect.Left := GetSystemMetrics(SM_XVIRTUALSCREEN);
Rect.Top := GetSystemMetrics(SM_YVIRTUALSCREEN);
Rect.Width := GetSystemMetrics(SM_CXVIRTUALSCREEN);
Rect.Height := GetSystemMetrics(SM_CYVIRTUALSCREEN);
DebugLn('Region: ', DbgS(Rect));
Bitmap := TBGRABitmap.Create(Rect.Width, Rect.Height, BGRABlack);
try
try
ScreenDC := GetDC(HWND_DESKTOP); // Get DC for all monitors
DebugLn('Start taking screenshot...');
Bitmap.LoadFromDevice(ScreenDC, Rect);
ReleaseDC(0, ScreenDC);
Bitmap.SaveToFile('screenshot.png');
DebugLn('Screenshot saved');
except
on E: exception do DebugLn('Error: ', e.tostring);
end;
finally
Bitmap.Free;
ReadLn;
end;
end.
It throws Cannot get raw image from device
exception when some of Rect
s values is negative. For example on this display configuration where primary display number 1 located to the right of the number 2:
Program output:
Region: l=-640,t=0,r=1736,b=752
Start taking screenshot...
Error: Exception: Cannot get raw image from device
Another example without error:
Program output:
Region: l=0,t=0,r=2376,b=752
Start taking screenshot...
Screenshot saved
Demo project: ScreenshotFailsDemo.zip
What did you expect
That the whole desktop would be captured.
A workaround was found in AutoScreenshot project.
Steps to reproduce
Run the test project.