Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to Capture Screen with Delphi code

design pattern

  • Please log in to reply
No replies to this topic

#1 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 03 December 2010 - 03:44 AM

Hello Guys,

In this tutorial we will talk about how to capture the screen content. I used the technique I will explain here in my freeware Image Captor. You can see how this technique implemented in real application by downloading and test Image Captor. Btw, it's using not only the technique explained in this tutorial, but also some methods explained in other tutorials here in CodeCall (the one about factory design pattern and another one about predefined folders in Windows).

In Windows, basically to capture the screen we need canvas of the topmost window. Remember that each window actually painted to its parent, and its parent painted to that parent's parent, and so on until you hit the topmost window. Hearing that may make you think that it's difficult to get to this ultimate parent window. Actually not! :-) Because it's simply the desktop window! And there is Windows API which immediately gives out the window handle of your current desktop.

So now our job is to get the canvas of the desktop window handle. Canvas (or TCanvas) is approximately the same with Device Context (DC) in Windows API terms. Then to get the "Canvas" we need to get the device context of desktop window. Again we are in luck here, since there already is Windows API to get device context of a given window handle. It's GetWindowDC.

Since we have all necessary information to compose our code, now is time to write the real code.

uses
  Windows   // we need it since we will use some WIndows APIs imported in this unit
  , Graphics  // unit where TBitmap is defined
  ;
  

// this procedure will capture the screen content into the suppliet ABitmap parameter.
procedure CaptureScreen(ABitmap: TBitmap)
var
  vDesktopDC: HDC;   // variable to store the device context handle of desktop window
begin
  // get the device context handle of current desktop window
  vDesktopDC := GetWindowDC(GetDesktopWindow);
  try
      // adjust the dimension and format of the supplied bitmap to match the screen
      ABitmap.PixelFormat := pf24bit;
      ABitmap.Height := Screen.Height;
      ABitmap.Width := Screen.Width;

      // draw the content of desktop into ABitmap
      BitBlt(ABitmap.Canvas.Handle, 0, 0, ABitmap.Width, ABitmap.Height, vDesktopDC, 0, 0, SRCCOPY);
  finally
    // mark that we have done with the desktop device context
    ReleaseDC(GetDesktopWindow, vDesktopDC);
  end;
end;

This procedure can be use for example like this (to save the captured screen into a JPG file).
uses
  ..
  , Jpeg
  ..

procedure CaptureScreenToFile(const AFilename: string);
var
  vJpg: TJpegImage;
  vBmp: TBitmap;
begin
  // create temporary bitmap
  vBmp := TBitmap.Create;
  try
    CaptureScreen(vBmp);
    // create Jpg image object
    vJpg := TJpegImage.Create;
    try
      vJpg.Assign(vBmp);
      // compress the image to have quality 70% of original
      vJpg.CompressionQuality := 70;
      // save the captured screen into a file in jpg format
      vJpg.SaveToFile(AFileName);
    finally
      vJpg.Free;  //destroy the jpg image object
    end;
  finally
    vBmp.Free; // destroy temporary bitmap
  end;
end;

And that's it! Ain't that easy?

Later I will update this tutorial with a screenshot sample, because now I have trouble with uploading attachment. Maybe it's my browser.

Cheers!

Edited by LuthfiHakim, 03 December 2010 - 08:38 PM.

  • 0





Also tagged with one or more of these keywords: design pattern

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download