2011. május 24., kedd
Screen capture of the desktop when multiple monitors are present
Problem/Question/Abstract:
I currently use DC := GetDC(GetDesktopWindow) to get a screen capture. What do I need to do do to capture the desktop when multiple monitors are present instead of just the primary monitor?
Answer:
Solve 1:
On multi-monitor systems the desktop is one big bitmap. On my dual-monitor system the following captures both desktops into one big bitmap. If you just wanted the second monitor you could adjust the left setting of the TRect to be captured. Keep in mind, however, that some people might arrange monitors other than side by side with monitor 1 on the left and monitor 2 on the right.
procedure GrabScreen(const SourceRect: TRect; Bitmap: TBitmap);
var
ScreenCanvas: TCanvas;
begin
ScreenCanvas := TCanvas.Create;
try
ScreenCanvas.Handle := GetDC(0);
try
Bitmap.Width := SourceRect.Right - SourceRect.Left;
Bitmap.Height := SourceRect.Bottom - SourceRect.Top;
Bitmap.Canvas.CopyRect(Rect(0, 0, Bitmap.Width, Bitmap.Height), ScreenCanvas,
SourceRect);
finally
ReleaseDC(0, ScreenCanvas.Handle);
ScreenCanvas.Handle := 0;
end;
finally
ScreenCanvas.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GrabScreen(Rect(0, 0, Screen.Monitors[0].Width + Screen.Monitors[1].Width,
Screen.Height),
Image1.Picture.Bitmap);
Caption := Format('Width %d Height %d', [Image1.Width, Image1.Height]);
end;
Solve 2:
Referring to Answer 1: Finally, I went through and used the debugger to check out all of the Screen variable fields at runtime and found what I needed:
{ ... }
DC := GetDC(GetDesktopWindow);
BMPImage := TBitmap.Create;
try
BMPImage.Width := Screen.DesktopWidth;
BMPImage.Height := Screen.DesktopHeight;
BitBlt(BMPImage.Canvas.Handle, 0, 0, BMPImage.Width, BMPImage.Height,
DC, Screen.DesktopLeft, Screen.DesktopTop, SRCCOPY);
finally
ReleaseDC(GetDesktopWindow, DC);
end;
{ ... }
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése