Capturing Windows Regardless of Their Z-Order

Introduction

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
{
   :
   public:
   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.



Downloads

Comments

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

    Posted by xnsua on 03/10/2017 04:51am

    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?

    Reply
  • Very Useful

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

    Thanx!

    Reply
  • the problem to do like this

    Posted by nehzil on 05/28/2007 02: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

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

    Posted by vinojkumar on 03/02/2007 01: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 09:43am

      Feel free to do it. Shahar

      Reply
    Reply
  • Thanks

    Posted by vlongsoft on 02/13/2007 10:00pm

    Thanks Shahar Golan, I am looking for this.

    Reply
  • Thanks

    Posted by vlongsoft on 02/13/2007 09:05pm

    Thanks Shahar Golan, I am looking for this.

    Reply
  • Great article

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

    Keep some more coming :)

    Reply
  • Capturing Windows when the machine is locked.

    Posted by c_c on 06/07/2006 06:46pm

    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

    Reply
  • Good Article

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

    Nice, I liked it. :)

    Reply
  • Great Shahar

    Posted by MrBeans on 05/24/2006 11:33am

    Lovely job done :thumb:

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

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