Mirror

Screen capture into a BMP file (Views: 100)


Problem/Question/Abstract:

Screen capture into a BMP file

Answer:

This little routine grabs the whole screen, assigns it temporary to a bitmap and stores it into file "sample.bmp".

A potential problem:
If your system is set up to HiColor (32k colors = 15 bits per pixel), some programs will not be able to read the result since they are only capable to read 16 bits/ pixel.


procedure TForm1.Button1Click(Sender: TObject);
var
  DeskTopDC: HDc;
  DeskTopCanvas: TCanvas;
  DeskTopRect: TRect;
  Bitmap: TBitmap;
begin
  DeskTopDC := GetWindowDC(GetDeskTopWindow);
  DeskTopCanvas := TCanvas.Create;
  DeskTopCanvas.Handle := DeskTopDC;
  DeskTopRect := Rect(0, 0, Screen.Width, Screen.Height);
  Bitmap := TBitmap.Create;
  with Bitmap do
  begin
    Width := Screen.Width;
    Height := Screen.Height;
    PixelFormat := pfDevice;
  end;
  Bitmap.Canvas.CopyRect(DeskTopRect, DeskTopCanvas, DeskTopRect);
  Bitmap.SaveToFile('c:\temp\sample.bmp');
  Bitmap.Free;
  DesktopCanvas.Free;
  ReleaseDC(GetDeskTopWindow, DeskTopDC);
end;

<< Back to main page