Author: William Gerbert
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.
1 procedure TForm1.Button1Click(Sender: TObject);
2 var3 DeskTopDC: HDc;
4 DeskTopCanvas: TCanvas;
5 DeskTopRect: TRect;
6 Bitmap: TBitmap;
7 begin8 DeskTopDC := GetWindowDC(GetDeskTopWindow);
9 DeskTopCanvas := TCanvas.Create;
10 DeskTopCanvas.Handle := DeskTopDC;
11 DeskTopRect := Rect(0, 0, Screen.Width, Screen.Height);
12 Bitmap := TBitmap.Create;
13 with Bitmap do14 begin15 Width := Screen.Width;
16 Height := Screen.Height;
17 PixelFormat := pfDevice;
18 end;
19 Bitmap.Canvas.CopyRect(DeskTopRect, DeskTopCanvas, DeskTopRect);
20 Bitmap.SaveToFile('c:\temp\sample.bmp');
21 Bitmap.Free;
22 DesktopCanvas.Free;
23 ReleaseDC(GetDeskTopWindow, DeskTopDC);
24 end;