Difference between revisions of "Screenshot"

From Lazarus wiki
Jump to navigationJump to search
Line 36: Line 36:
  
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
procedure ScreenshotToFile(Filename: string; Monitor: integer);
 
 
var
 
var
   BMP: Graphics.TBitmap; // graphics.TBitmap, not Windows.TBitmap
+
   BMP: Graphics.TBitmap;
 
   ScreenDC: HDC;
 
   ScreenDC: HDC;
 
   M: TMonitor;
 
   M: TMonitor;
 
   W, H, X0, Y0: integer;
 
   W, H, X0, Y0: integer;
 
begin
 
begin
  // Initialize coordinates of full composite area
 
 
   X0 := Screen.DesktopLeft;
 
   X0 := Screen.DesktopLeft;
 
   Y0 := Screen.DesktopTop;
 
   Y0 := Screen.DesktopTop;
   W := Screen.DesktopWidth;
+
   W := Screen.DesktopWidth;
   H := Screen.DesktopHeight;
+
   H := Screen.DesktopHeight;
 
   // Monitor=-1 takes entire screen, otherwise takes specific monitor
 
   // Monitor=-1 takes entire screen, otherwise takes specific monitor
 
   if (Monitor >= 0) and (Monitor < Screen.MonitorCount) then begin
 
   if (Monitor >= 0) and (Monitor < Screen.MonitorCount) then begin
Line 66: Line 64:
 
   BitBlt(BMP.Canvas.Handle, 0, 0, W, H, ScreenDC, X0, Y0, SRCCOPY);
 
   BitBlt(BMP.Canvas.Handle, 0, 0, W, H, ScreenDC, X0, Y0, SRCCOPY);
 
   ReleaseDC(0, ScreenDC);
 
   ReleaseDC(0, ScreenDC);
  // save to file (possibly to TStream, etc.)
 
  BMP.SaveToFile(Filename);
 
  BMP.Free;
 
end;
 
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 23:04, 22 January 2022

Example codes create the screenshot in the TBitmap object. You can then save this bitmap to a file. Bitmap takes the entire desktop area, including all monitors.

Example 1

Code from forum user fripster. It supports Windows and Linux.

procedure TakeScreenShot(b: Graphics.TBitmap);
{$IFDEF WINDOWS}
var
  ScreenDC: HDC;
begin
  b.Width:= Screen.DesktopWidth;
  b.Height:= Screen.DesktopHeight;
  b.Canvas.Brush.Color:= clWhite;
  b.Canvas.FillRect(0, 0, b.Width, b.Height);
  ScreenDC:= GetDC(GetDesktopWindow);
  BitBlt(b.Canvas.Handle, 0, 0, b.Width, b.Height, ScreenDC, Screen.DesktopLeft, Screen.DesktopTop, SRCCOPY);
  ReleaseDC(0, ScreenDC);
end;
{$ENDIF}
 
{$IFDEF LINUX}
var
  ScreenDC: HDC;
begin
  ScreenDC:= GetDC(0);
  b.LoadFromDevice(ScreenDC);
  ReleaseDC(0,ScreenDC);
end;
{$ENDIF}

Example 2

Code from forum user Manlio. With Monitor=-1 it takes the whole desktop area, otherwise it takes only specific monitor.

var
  BMP: Graphics.TBitmap;
  ScreenDC: HDC;
  M: TMonitor;
  W, H, X0, Y0: integer;
begin
  X0 := Screen.DesktopLeft;
  Y0 := Screen.DesktopTop;
  W := Screen.DesktopWidth;
  H := Screen.DesktopHeight;
  // Monitor=-1 takes entire screen, otherwise takes specific monitor
  if (Monitor >= 0) and (Monitor < Screen.MonitorCount) then begin
    M  := Screen.Monitors[Monitor];
    X0 := M.Left;
    Y0 := M.Top;
    W  := M.Width;
    H  := M.Height;
  end;
  // prepare the bitmap
  BMP := Graphics.TBitmap.Create;
  BMP.Width  := W;
  BMP.Height := H;
  BMP.Canvas.Brush.Color := clWhite;
  BMP.Canvas.FillRect(0, 0, W, H);
  ScreenDC := GetDC(GetDesktopWindow);
  // copy the required area:
  BitBlt(BMP.Canvas.Handle, 0, 0, W, H, ScreenDC, X0, Y0, SRCCOPY);
  ReleaseDC(0, ScreenDC);