Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

How to Capture Screen with Delphi code

design pattern

  • Please log in to reply
No replies to this topic

#1 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.

  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)
  vDesktopDC: HDC;   // variable to store the device context handle of desktop window
  // get the device context handle of current desktop window
  vDesktopDC := GetWindowDC(GetDesktopWindow);
      // 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);
    // mark that we have done with the desktop device context
    ReleaseDC(GetDesktopWindow, vDesktopDC);

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

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

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.


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