1. Home
  2. Computing & Technology
  3. Delphi Programming

Desktop Screen Shot using Delphi Code
Capture Desktop Image into a TBitmap object

By Zarko Gajic, About.com

Need to take a Windows Desktop screen shoot from Delphi code? No problem ... here's how to capture the Windows Desktop image into a TBitmap object - and display it inside a TImage control.

Drop a TImage (named "Image1") on a form and use the following code:

procedure ScreenShot(DestBitmap : TBitmap) ;
var
  DC : HDC;
begin
  DC := GetDC (GetDesktopWindow) ;
  try
   DestBitmap.Width := GetDeviceCaps (DC, HORZRES) ;
   DestBitmap.Height := GetDeviceCaps (DC, VERTRES) ;
   BitBlt(DestBitmap.Canvas.Handle, 0, 0, DestBitmap.Width, DestBitmap.Height, DC, 0, 0, SRCCOPY) ;
  finally
   ReleaseDC (GetDesktopWindow, DC) ;
  end;
end;
Note: the width and height of the screen shot are equal to the width and height of the physical screen.

Usage:

var
b:TBitmap;
begin
b := TBitmap.Create;
try
ScreenShot(b) ;
Image1.Picture.Bitmap.Assign(b) ;
finally
b.FreeImage;
FreeAndNil(b) ;
end;
Delphi tips navigator:
» Hide a Delphi Application Button from the TaskBar
« MORE TIPS
More Delphi Programming Quick Tips
Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 1999 Delphi Tips
  7. Capture Desktop Image: Screen Shot using Delphi Code

©2009 About.com, a part of The New York Times Company.

All rights reserved.