Capture the window bitmap from another running program.

Posted on 2004-10-26
Last Modified: 2013-12-03
I have a Visual C++ 6.0 program which needs to capture the client area bitmap from the window of another running application.  I have the window handle of the target window am able to get the bitmap if the target window is completely visible, but if the window is partially overlayed, then the bitmap capture gets the overlaying windows as well.  I have read here and other sites that sending a WM_PRINT message to the target window and providing a device context will do this, but so far I have been unable to get this to work.  The code I am using is the following:

        CRect rect;
        CDC   memDC;

        CWnd* pWindow = CWnd::FromHandle( m_hWindow );

        if ( ( pWindow != NULL ) && ( IsWindow( pWindow->m_hWnd ) == TRUE ) )
            CDC* pClientDC   = new CClientDC( pWindow );
            CBitmap* pBitmap = new CBitmap();
            pWindow->GetClientRect( &rect );
            pBitmap->CreateCompatibleBitmap( pClientDC, rect.Width(), rect.Height() );

            memDC.CreateCompatibleDC( pClientDC );
            CBitmap* pOldBitmap = memDC.SelectObject( pBitmap );

            // Adding this line and removing the next line did not work
            //SendMessage( m_hWindow, WM_PRINT, ( WPARAM )memDC.GetSafeHdc(), PRF_CLIENT );

            // This code captures the overlayed windows
            memDC.BitBlt( 0, 0, rect.Width(), rect.Height(), pClientDC, 0, 0, SRCCOPY );

            memDC.SelectObject( pOldBitmap );

            ReleaseDC( m_hWindow, pClientDC->GetSafeHdc() );
            delete pClientDC;

What do I need to modify in this code so that the target window bitmap is captured even if the window is partially overlayed.
Question by:MugWumpBW
    LVL 55

    Expert Comment

    by:Jaime Olivares
    have a look to this tutorial:

    Expert Comment

    If you don't mind the target window to come in and out of focus for an instant, you can do:

    if ( ( pWindow != NULL ) && ( IsWindow( pWindow->m_hWnd ) == TRUE ) )
        HWND currentForegroundWindow = GetForegroundWindow();

    Author Comment

    The only problem with that approach is that I capture the bitmaps every 500 ms so that would effectively cause the target window to remain on top at all times - which is what I am trying to avoid.
    LVL 1

    Accepted Solution


    Not possible without hacking.


    > The implementation of this seemly simple class is quite tricky.
    > Some knowledge of Win32 API implementation, compiler code generation,
    > Intel machine code, and virtual memory is needed to understand it fully.


    > Limitation
    > The Hook function only handles exported function whose first instruction is "MOV EAX, <DWORD_constant>".
    > So the implementation shown here only applies to Windows NT/2000.
    > Refer to Chapter 4 for more generic of restrictive API hooking solutions.
    > Only tested on Windows 2000 machine.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now