Barry's Screen Capture

Environment: VC6 SP3, Windows 98

Introduction

This article demonstrates the methods of capturing different portions of the screen. You have the facility to then save the captured image to a file.

The idea for developing the article came to me when I had downloaded a utility for capturing screen shots for use in my articles like this one, on the Internet, but unfortunately the downloaded utility had too many nagging screens and obviously for a no-nags version I had to pay (which I cannot afford), so the only way out was to develop one on my own utility. The MSDN sample WinCap could have served the purpose, but the source was not available on the CD so I stared developing the article in MFC using the Win32 sample provided on the MSDN CD (WinCap). Also, I can capture portions of the screen, which I had done some time ago in Visual Basic because screen capture in Visual Basic is much easier and code for this is available in the Knowledge Base section of Visual Basic.

Features

This article has the following features:

  • Capture the Desktop
  • Capture a Window
  • Capture a Client Area
  • Capture a Control (Button)

In addition, it has the following intresting, but not so useful (in my opinion), features:

  • Track the X and Y coordinates of the captured image
  • Trace the color code of a specific point of the captured image

Options for color and position tracking are on the MainFrame Window of the system and can only be used after capturing an image.

How to Use the Project

Download the project source, unzip the files to some folder, and build the project. In case there are no errors at build-time, execute the project; an icon of a camera will appear in the System Tray (bar at the bottom of the screen). Right-click the the icon and a menu will pop up. Select the options from the menu.

Pop-up Menu

OptionTask performed
Capture DesktopCaptures an image of the System Desktop immediately
Capture WindowPrompts to click on window to capture
Capture Client AreaPrompts to click on client area to capture
Capture ControlPrompts to click on control to capture
Clear ImageClears captured image
Show WindowDisplays the CapIT System Window
CloseCloses the CapIT System

In addition to the pop-up menus, the CapIT System has it own main menu, which can be displayed when you select "Show Window" from the pop-up menu.

How it Works

When capturing a window, client area, or control, the system calls the function SetCapture() and waits for the user to click on some window on the Desktop which is to be captured. After clicking on the area to be captured, the system framework calls the member function void CCapITView::OnCaptureChanged(CWnd *pWnd), which calls WindowCapture(HWND hwnd), which in turn carries out the job of capturing the image (until the menu is disabled).

After completing the capture, the system calls ReleaseCapture() to release SetCapture(). But, in the case of capturing the Desktop, SetCapture() is not called, but directly calls WindowCapture(HWND hwnd), passing to it GetDesktopWindow()->m_hWnd, and the system proceeds to capture the Desktop image immediately because there is only one Desktop area and its HWND is known to the system.

Note: The following member functions in the class CCapITView are borrowed from the MSDN sample WinCap. Could I have written them on my own? No way!

Known Problems

The color of the saved image under Windows 98 is not the same as of the captured image, but I think it is nearly the same in Windows 2000.

Points of Interest

The project uses CScrollView instead of CView because, if the image being captured is bigger in size (for example, the Desktop), the Frame Window may not be able to capture the entire image because CView will capture as per the CView Client Area size.

HBITMAP CopyWindowToBitmap(CWnd* wnd , HWND hWnd, WORD fPrintArea)
HBITMAP CopyScreenToBitmap(LPRECT lpRect)
BOOL PaintBitmap(HDC hDC, LPRECT lpDCRect, HBITMAP hDDB,
                          LPRECT lpDDBRect, HPALETTE hPal)
void DoSize(CWnd* wnd , HBITMAP ghBitmap)
void SetupScrollBars(CWnd* wnd, WORD cxBitmap, WORD cyBitmap)
void ReallyGetClientRect(CWnd* wnd, LPRECT lpRect)
WORD SaveDIB(HDIB hDib, LPSTR lpFileName)
WORD PaletteSize (VOID FAR * pv)
WORD DibNumColors (VOID FAR * pv)
HDIB ChangeBitmapFormat(HBITMAP hBitmap, WORD wBitCount,
                        WORD dwCompression, HPALETTE hPal,
                        HWND hwnd)
HANDLE AllocRoomForDIB(BITMAPINFOHEADER bi, HBITMAP hBitmap,
                       HWND hwnd)
HPALETTE GetSystemPalette(HWND hwnd)
int PalEntriesOnDevice(HDC hDC)

Downloads

Download demo project - 12 Kb
Download source - 46 Kb


Comments

  • The code is here

    Posted by zerver on 08/20/2008 09:46am

    http://www.codeguru.com/code/legacy/gdi/CapIT_src.zip

    Reply
  • Slight palette bug

    Posted by zerver on 07/10/2007 11:27am

    Great article! Because the wrong palette is accidentally being used when calculating the required size of the bitmap, the File->Save command will sometimes fail. Solution: Select the correct palette into hDC before call to GetDIBits in function AllocRoomForDIB. Or even better, avoid unnecessary calls to CreateDC by passing the hDC handle directly to AllocRoomForDIB.

    Reply
  • the link for source code is missing

    Posted by vikasamin on 11/24/2005 09:49am

    and even the picuture that is save is of poor quality

    Reply
  • Latest Updates

    Posted by barretto_vn on 03/15/2005 08:51am

    for updates to this and more of my articles go to http://members.lycos.co.uk/barrettovn Barretto VN

    Reply
  • capturing the cursor

    Posted by eligeti on 03/06/2005 01:24pm

    Hi,
    the above capture wont capture th cursor. How can i do that? i found that one can get the cursor info by GetCursorInfo() and then use DrawIcon() to draw on it. but how can i get the cursor icon?

    Reply
  • I can't download source. help me

    Posted by pronong on 03/04/2004 11:45am

    I wanted source code. but can't not download.

    • Source Location

      Posted by keedo60 on 05/01/2004 07:16pm

      You can get source here.

      Reply
    Reply
  • How can i capture DirectX that game screen ?

    Posted by Legacy on 01/26/2004 12:00am

    Originally posted by: LeeSeugHun

    How can i capture DirectX that game screen like hiper snap?

    good luck.

    Reply
  • Capturing inactive window?

    Posted by Legacy on 01/23/2004 12:00am

    Originally posted by: Michael

    Is it possible to capture a window if it is minimized?

    Reply
  • How to capture images from media player as it plays?

    Posted by Legacy on 04/17/2003 12:00am

    Originally posted by: Thomas J. Clancy

    Just wondering something... Do you (the author) know how to make your screen capture app capture images as they're played via microsoft's media player? Whenever you capture a window or the desktop, the media player's picture are is always blank. I think this has something to do with direct x and I'm trying to find a way to capture a desktop image including something that is drawing via directx on a region of the screen.

    Tom Clancy

    Reply
  • Where is my memory!

    Posted by Legacy on 03/19/2003 12:00am

    Originally posted by: Thomas Rokohl

    hello,

    nice example programm, but there is a "memory-eater"
    in your programm. so, take a look at the (global-system)memory and capture the desktop again and again, you see that the programm need's more and more memory!
    i can't find the place where it is?!

    i someone has found it please send me a email!
    Thanx

    Thomas Rokohl

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds