2011. május 24., kedd

Screen capture of the desktop when multiple monitors are present


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?


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);
  ScreenCanvas: TCanvas;
  ScreenCanvas := TCanvas.Create;
    ScreenCanvas.Handle := GetDC(0);
      Bitmap.Width := SourceRect.Right - SourceRect.Left;
      Bitmap.Height := SourceRect.Bottom - SourceRect.Top;
      Bitmap.Canvas.CopyRect(Rect(0, 0, Bitmap.Width, Bitmap.Height), ScreenCanvas,
      ReleaseDC(0, ScreenCanvas.Handle);
      ScreenCanvas.Handle := 0;

procedure TForm1.Button1Click(Sender: TObject);
  GrabScreen(Rect(0, 0, Screen.Monitors[0].Width + Screen.Monitors[1].Width,
  Caption := Format('Width %d Height %d', [Image1.Width, Image1.Height]);

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;
  BMPImage.Width := Screen.DesktopWidth;
  BMPImage.Height := Screen.DesktopHeight;
  BitBlt(BMPImage.Canvas.Handle, 0, 0, BMPImage.Width, BMPImage.Height,
    DC, Screen.DesktopLeft, Screen.DesktopTop, SRCCOPY);
  ReleaseDC(GetDesktopWindow, DC);
{ ... }

Nincsenek megjegyzések:

Megjegyzés küldése