Virtual Developer Workshop: Containerized Development with Docker


This article demonstrates how to capture top-level windows with the WS_VISIBLE style, regardless of their z-order.

Functionality Included

  • Capture all the screen content to a BMP file.
  • Capture the foreground window content to a BMP file.
  • Capture the covered window content to a BMP file.

All functionality is implemented inside the CaptureEx class and, in addition to capturing screen content, one can find other useful functionality of the class, such as:

  • Creating (saving) a BMP file from raw bits.
  • Converting any input HBITMAP into 24-bit raw bits.
  • Stretching (zoom in/out) BMP raw bits to a given window handle.

About the Project

Capturing window content can be done easily by using the ::BitBlt(..) from the screen DC; however, this kind of implementation forces you to make sure that the window you want to capture is on top of the screen. In this project, to capture all the screen (Desktop), this technique is useful. Because it's not good for capturing covered windows, I capture windows with the ::PrintWindow(..) API.

The project is a MFC-dialog based application, built with VC6. However, as mentioned above, all the implementations are inside the CaptureEx class, so actually MFC is being used just as a wrapper for the GUI; the CaptureEx functionality is implemented with the Win32 API. In that way, Win32 applications can also benefit from the class.

How It Works

When the application starts running, it uses ::EnumWindows(..) to build a list of all top/visible windows; each valid window for capturing is added to the list. Clicking "Refresh Windows List" will update the windows list.

Once you have all the windows in the list, you can select a window from the list (using the mouse/arrow keys); each selected window will be captured and displayed on the dialog. The capture is made without changing the window's Z-order.

You can change the preview slider to zoom in/out the captured window image; clicking "Show actual size" will display the captured window image at its real size.

To save a particular captured window to a BMP file, press the "Save window to Bmp" button.

All captured windows will be saved at their real size, no matter how the preview is set.

Note: Capturing minimized windows (Iconic) will only generate the caption image of the window. Here is a sample of Iconic window capture:

The CaptureEx Class

Useful functions of the CaptureEx class are shown in this code snippet:

class CaptureEx
   void SaveBmpToFile(LPTSTR szFileName, int W,int H,
                      int Bpp, int* lpBits);
   void Get24BitBmp ( const int &nWidth,const int &nHeight,
                      const HBITMAP &hBitmap, BYTE *lpDesBits);
   void CaptureDesktop (double fPreviewRatio,
                        LPTSTR lpszFileName,
                        BOOL bSaveToFile);
   void StretchDIBitsToHwnd( HWND hWnd, double fRatio,
                             int Width, int Height,int Bpp,
                             BYTE *lpBits);
   BOOL CaptureWindow( HWND hWndSrc, double fPreviewRatio,
                       LPTSTR lpszFileName,BOOL bSaveToFile);

Known Issue

During the development of the application, I ran into a problem with capturing IE windows; sometimes, part of the image goes black. I assume it's due to the way IE windows repaint themselves. At the moment, the solution I came up with is to capture each window a couple of times and get the correct image by using image filtering.



  • Can I change the loop of 5(magic number?) in function "CaptureWindow"

    Posted by xnsua on 03/10/2017 12:51pm

    I don't understand the meaning of 5. I think when BlackPixels is the same as last capture, The capture could be considered as a success. Is it true?

  • Very Useful

    Posted by HanneSThEGreaT on 09/11/2007 11:32am


  • the problem to do like this

    Posted by nehzil on 05/28/2007 09:43am

    I try this method, but if i do this on timer, the captured window will not display normally, i don't know why and how to solve it? thanks

  • Can I use 2 sub-routines Get24BitBmp & CaptureDesktop in my app?

    Posted by vinojkumar on 03/02/2007 09:22am

    Hi, Can I use the 2 sub-routines. This is not documented elsewhere so I used GetPixel to get the full screen. Now I want to use your 2 sub-routines altered to my needs. The project is NetMonitor and it is going to appear in Codeguru.com in another few weeks or so. I will give you commented credits in the sources of mine. Excellent work. My Link in codeguru is List Of Gurus->V->Vinoj Kumar and I have written many big articles. Thanks, Regards, Vinoj

    • Sure you can :-)

      Posted by golanshahar on 03/02/2007 05:43pm

      Feel free to do it. Shahar

  • Thanks

    Posted by vlongsoft on 02/14/2007 06:00am

    Thanks Shahar Golan, I am looking for this.

  • Thanks

    Posted by vlongsoft on 02/14/2007 05:05am

    Thanks Shahar Golan, I am looking for this.

  • Great article

    Posted by kirants on 07/30/2006 07:33pm

    Keep some more coming :)

  • Capturing Windows when the machine is locked.

    Posted by c_c on 06/08/2006 01:46am

    I am getting black windows while trying to capture windows using PrintWindow when the machine is locked. As per the LockWorkStation API the window will not be visible when the machine is locked. Any suggestion on getting around this issue. Thanks -CC

  • Good Article

    Posted by Krishnaa on 06/05/2006 08:21pm

    Nice, I liked it. :)

  • Great Shahar

    Posted by MrBeans on 05/24/2006 06:33pm

    Lovely job done :thumb:

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date