Screen Capturing via GDI+ and GDI

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
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
August 06, 2008
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

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.

GDI’s Role in Screen Capturing

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+.

Developing a Screen Capture Application

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:

void CScreenCaptureDlg::OnMouseMove(UINT nFlags, CPoint point)

{

//if the user press the left mouse button and does not release it (i.e. drag), then start to capture

if(nFlags==MK_LBUTTON){

//hide the demo application window in order not to affect the vision of the mouse capturing

ShowWindow(SW_HIDE);

//load the camera shaped mouse cursor, and start to track the movement of the mouse cursor

HCURSOR cur=LoadCursor(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDC_CAMERA));

SetCursor(cur);

SetCapture();

//obtain the handler of the window that corresponds to the current mouse cursor

this->ClientToScreen(&point);

hwndCapture=(HWND)::WindowFromPoint(point);

//obtain the handler of the device context of the screen so as to draw at any positon on the screen

HDC hDC=::GetDC(NULL);

//create a new red paint brush

HPEN hPen=CreatePen(PS_INSIDEFRAME,6,RGB(255,0,0));

//set the draw mode to R2_NOTXORPEN so as not to destroy the original background color

int nMode=SetROP2(hDC,R2_NOTXORPEN);

HPEN hpenOld=(HPEN)SelectObject(hDC,hPen);

//get the rectangle of the window relating to the current mouse cursor

::GetWindowRect(hwndCapture,&rectCapture);

//draw a red rectangle around the window relating to the current mouse cursor to friendly prompt the user

POINT pt[5];

pt[0]=CPoint(rectCapture.left,rectCapture.top);

pt[1]=CPoint(rectCapture.right,rectCapture.top);

pt[2]=CPoint(rectCapture.right,rectCapture.bottom);

pt[3]=CPoint(rectCapture.left,rectCapture.bottom);

pt[4]=CPoint(rectCapture.left,rectCapture.top);

::Polyline(hDC,pt,5);

//first sleep 100 milliseconds and then redraw the red rectangle so as not to destroy the original content

Sleep(100);

::Polyline(hDC,pt,5);

//seemly verbose while effective

::SelectObject(hDC,hpenOld);

::ReleaseDC(NULL,hDC);

}

CDialog::OnMouseMove(nFlags, point);

}

void CScreenCaptureDlg::OnLButtonUp(UINT nFlags, CPoint point)

{

// 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

hBitmap=CreateCompatibleBitmap(hdcScreen,nWidth,nHeight);

//select the newly-built bitmap into the memory device context

hOldBitmap=(HBITMAP)SelectObject(hMemDC,hBitmap);

//copy the screen device context into the memory device context

BitBlt(hMemDC,0,0,nWidth,nHeight,hdcScreen,rectCapture.left,rectCapture.top,
SRCCOPY);

DeleteDC(hdcScreen);

DeleteDC(hMemDC);

//return the handler of the bitmap

// Open the clipboard and copy the bitmap to it

OpenClipboard();

EmptyClipboard();

SetClipboardData(CF_BITMAP,hBitmap);

// Close the clipboard

CloseClipboard();

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.

Persisting the Captured Images via GDI+

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:

void CScreenCaptureDlg::OnLButtonUp(UINT nFlags, CPoint point)

{

//……omitted (see the download source code)

if(GetSaveFileName(&ofn))

{

CLSID pngClsid;

Bitmap bmp(hBitmap,NULL);

//get the encode mode of the BMP file

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 itfor detailed coding you can refer to the source code at the end of the article.

Adding a Reference to GDI+ and Visualizing the Result

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:


Gdiplus::GdiplusStartupInput gdiplusStartupInput;

Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);


(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.

-DOWNLOAD SOURCE-

blog comments powered by Disqus
WINDOWS SCRIPTING ARTICLES

- More Windows Scripting Workarounds from Nilpo
- Overloading Methods and More in VBScript
- Improving MFC for Windows Vista
- Regular Expressions in VBScript
- Working with Dates in WMI
- Completing Calendars with VBScript Date Func...
- Building Calendars with VBScript Date Functi...
- Working With Dates and Times in VBScript
- Designing WCF DataContract Classes Using the...
- Understanding Dates and Times in VBScript
- Working With Arrays in VBScript
- Compressed Folders in WSH
- Using .NET Interops in VBScript
- Nilpo`s Scripting Secrets, Vol I
- Database operations using Silverlight 2.0 WC...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials