In this article, the hidden mechanisms of popular screen capture applications are unveiled. I will examine the pros and cons of GDI+ and GDI and develop a typical screen capture application in Visual C++ 2005.
Contributed by Xianzhong Zhu Rating: / 3 August 06, 2008
Screen capture applications are widely used for handling pictures and images. As far as image processing in Windows XP and later versions, GDI+ has vastly assisted traditional GDI developers in processing 2-D vector graphics, rendering and transforming images, as well as dealing with typography. Nevertheless, GDI+ has not been able to take the place of GDI. Compared with GDI, GDI+ bears at least the following deficiencies:
Does not support the bitblt opertion as provided under the GDI environment
Does not support the raster related bit operation
When performance and speed are a must, GDI is much better than GDI+ for high performance and quality
Author's Note: To follow along with the sample in this article, you're assumed to have installed the following environments: Windows XP Professional and Visual Studio 2005.
On the whole, capturing the screen requires two crucial techniques: one is to obtain the handler of the window in which your picture lies (i.e. making clear what to capture). The other is to persist the captured image. This is the strong point of GDI+.
As for the first technique, we can resort to the SetCapture() function to track the mouse moving opertion. During the movement of the mouse, the SetCapture() function can obtain the handler of the window that the mouse points to. Moreover, we can also use the WindowFromPoint() function to find the corresponding window's handler.
Readers who are familiar with SnagIT know that when capturing some window there is usually a red rectangle around the target window to remind users of the selected window. This functionality is more complex than the former. Next, I’ll show you how to draw a red rectangle using GDI related techniques.
A very fundamental concept in GDI is the device context (DC), which is what is owned by each window. Suppose the DC of a window is retrieved. Users can render at any part of the window. However, in a typical screen capture application, the target window that users select is uncertain. Therefore, it is not that easy for developers to obtain the DC of the window at the location of the mouse cursor. In fact, the key to this lies in the GetDC() function. The signature is given below:
HDC GetDC(HWND hWnd);
The hWnd parameter corresponds to the handler of the window that’s related to the current DC object. Note that when hWnd is null, the GetDC() functionreturns the handler of the device context of the screen. This means that developers can draw anything at any position on the screen.
The purpose of drawing is to remind users of the currently-selected window. So when drawing, we have to make sure we don't destroy the original content. In such a case, we can set the render mode of the window to RS_NOTXORPEN. First, this makes the paint brush color XOR computed with the screen color, and then NOT computed with the screen color. The same pixel will stay unaltered after being computed via RS_NOTXORPEN twice. In this way, we can draw in the RS_NOTXORPEN node twice, because the window will stay unchanged.
Author's Note: all the above requirements are rather difficult to accomplish via GDI+.
A screen capture application contains at least three steps:
(1) Capture mouse cursor
(2) Allow the user to draw the specified target
(3) Save the target window as a custom bitmap and terminate the operation that captures the mouse movement.
Now, let’s compose the sample application:
(1) Launch Visual Studio 2005, select Visual C++ to create a MFC styled dialog based project, and name it ScreenCapture. Then, to gain a better visual effect, download or manually compose a camera cursor file (*.cur), import it into the project, and name it IDC_CAMERA. After that, declare two public variables within the CscreenCaptureDlg class as follows:
public:
HWND hwndCapture;
CRect rectCapture;
hwndCapture is used to hold the handler of the target window that is to be captured and rectCapture corresponds to the rectangle of the capturing window.
(2) Define the two (WM_MOUSEMOVE and WM_LBUTTONUP) event handlers via the wizard. The complete source code of the two event handlers is listed below:
// obtain the width and height of the window the mouse cursor points to
int nWidth=rectCapture.Width();
int nHeight=rectCapture.Height();
HDC hdcScreen,hMemDC;
HBITMAP hBitmap,hOldBitmap;
//set up a handler of the device context of the screen
hdcScreen=CreateDC("DISPLAY",NULL,NULL,NULL);
hMemDC=CreateCompatibleDC(hdcScreen);
// set up a bitmap that is compatible with the above handler of the screen device context and has the same size as the window the current mouse cursor points to
MessageBox("The specified content has been copied to the clipboard!");
ReleaseCapture();
//restore the original state of the demo applicaion’s window
ShowWindow(SW_NORMAL);
CDialog::OnLButtonUp(nFlags, point);
}
Since there are already detailed explanations in the above source code, we don't need to give uncecessary details. The core module of a professional screen capturing application has been accomplished.
As soon as the user starts the application and presses the left button of the mouse in the dialog, the capturing begins. After the captured target is properly selected and the left button of the mouse is released, the image of the target window will be persisted automatically into the system clipboard. However, persisting the corresponding image to the hard disk is more important. Apparently, if we take the GDI path, we need to be quite familiar with the structures of various bitmaps. This leads to so much trouble. On the other hand, with GDI+, everything becomes so convenient and natural. Let’s take a look at how to save the captured image into a .bmp file.
So far, the capturing application has gotten the handler of the bitmap in relation to the captured window. Now, we are to save the handler of the bitmap as the expected bitmap file. All in all, this will be performed with the help of the Bitmap class in GDI+. This is shown below:
GetEncoderClsid(L"image/bmp",&pngClsid);//helper function
CString tmp(ofn.lpstrFile);
CStringW filename((LPCSTR)tmp);
//store the captured picture on the screen
bmp.Save(filename,&pngClsid);
}
ReleaseCapture();
MessageBox("The content on the screen has been successfully saved into a disk file!");
// restore the original state of the demo applicaion’s window
ShowWindow(SW_NORMAL);
CDialog::OnLButtonUp(nFlags, point);
}
Please notice that we construct another helper function, called GetEncoderClsid, which is responbile for retrieving the CLSID info of the encoder of the image file in the specified format. To cut a long story short, we will no longer dwell on it—for detailed coding you can refer to the source code at the end of the article.
VC++ is different from VC# (which provides built-in support for GDI+). A VC++ based project has to first add a proper reference to the GDI+ library in order to leverage all the functions in GDI+. In this case, we should consider the following steps:
(1) Open the head file stdafx.h and append the three lines below to the end of it:
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;
(2) Define a variable member for the CscreenCaptureApp class as follows:
ULONG_PTR m_gdiplusToken;
(3) Initialize the GDI+ library within the InitInstance() method of the CscreenCaptureApp class before using it:
(4) Perform the necessary cleanup within the ExitInstance() method of the CscreenCaptureApp class with the related info used in GDI+:
Gdiplus::GdiplusShutdown(m_gdiplusToken);
Now, with everything ready you can leverage all the above functions provided in GDI+. Let’s start the demo application and see what happens. Press F5 to launch the application. You will see a common dialog, as is shown in Figure 1.
Figure 1— the initial running-time snapshot of the screen capturing application
Just press the left button of the mouse and try to drag; there will be a red rectangle appearing around the window the current cursor points to. When the user releases the left button, the image of the present window will be copied into the system clipboard and a dialog will appear to prompt you to specify the path to persist the image. Figure 2 gives a running-time snapshot when the mouse moves over the window of the FireFox browser.
Figure 2—move the mouse cursor and capture some window in the red rectangle
Because of the shortage of the tool I used to capture the screenshot, it seems the camera shaped mouse cursor is not in Figure 2. Never mind—you can surely catch sight of it in the real sample application.
Summary
In this article, we examined the core module of a screen capture application under Windows via GDI and GDI+. Readers should now have a clearer understanding when composing graphic applications.
On the other hand, GDI+ is expected to become the new graphic engine for the next generation.