1. Computing & Technology

Discuss in my forum

Desktop Screen Shot using Delphi Code

Capture Desktop Image into a TBitmap object

By , About.com Guide

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

©2012 About.com. All rights reserved.

A part of The New York Times Company.