This article is translated from P.gopalakrishna 's various methods for capturing to the screen, see below for the original address. The copyright of this article is owned by the original author.
If the translation is reproduced, please ensure the integrity of the article and indicate from www.farproc.com
Shaohui
2005/6/12
Original address: Http://www.codeproject.com/dialog/screencap.asp#Windows%20Media%20API%20for%20Capturing%20the%20Screen%20 :
This article comes with source 1 download 39K
This article comes with source 2 download 135.5K
This article comes with source 3 download 59.8K
Directory:
L Introduction
L Use the GID function to grab the screen
L Catch screen with DirectX method
• Grab screen with Windows Media API
Introduction
There are times when we need to programmatically crawl the entire screen, and I'll explain how the screenshot is implemented. Typically, we can do it with GID and DirectX, and the other option is the Windows Media API, which I'll analyze each one. In each method, once we have the contents of the screen stored in the program defined memory block or bitmap file, we can further use them to generate animation and movies, the process you can refer to the "Create a movie from Hbitmap" in the article to get more help.
Grabbing a screen with a GDI function
If we don't care much about the efficiency of the capture screen, and we want just a screenshot, consider using the GDI method. This capture mechanism is based on the simple common sense that "the desktop is also a window, the desktop also has a window handle (HWND)", and if we get the device context (DC) of the desktop, we can use Blit (copy) its contents to the DC we created. We can use Getdeskwindow () to get the window handle of the desktop, and it is easy to get the DC from the clause handle. The implementation steps are:
1. Using the GetDesktopWindow () function to get the window handle of the desktop
2. DC using GetDC () to get the desktop window
3. Create and screen DC-compatible bitmaps and DCs (CreateCompatibleBitmap () and CreateCompatibleDC ()), and select this bitmap into the DC (SelectObject ())
4. When you are ready to grab the screen, copy the contents of the desktop window DC to the compatible DC, you will complete the capture process, compatible with the bitmap is the screen content
5. Do not forget to release the object you created, memory is valuable (for other programs)
Sample code:
void Capturescreen ()
{
int nscreenwidth = GetSystemMetrics (Sm_cxscreen);
int nscreenheight = GetSystemMetrics (Sm_cyscreen);
HWND Hdesktopwnd = GetDesktopWindow ();
HDC HDESKTOPDC = GetDC (Hdesktopwnd);
HDC Hcapturedc = CreateCompatibleDC (HDESKTOPDC);
Hbitmap hcapturebitmap =createcompatiblebitmap (HDESKTOPDC,
Nscreenwidth, Nscreenheight);
SelectObject (HCAPTUREDC,HCAPTUREBITMAP);
BitBlt (hcapturedc,0,0,nscreenwidth,nscreenheight,hdesktopdc,0,0,srccopy);
Savecapturedbitmap (HCAPTUREBITMAP); Place Holder-put Your code
Here to save the captured image to disk
ReleaseDC (HDESKTOPWND,HDESKTOPDC);
DeleteDC (HCAPTUREDC);
DeleteObject (HCAPTUREBITMAP);
}
In the code snippet above, GetSystemMetrics () returns the width (sm_cxscreen) and height (sm_cyscreen) of the screen. For information on how to save captured bitmaps to files and how to place them on the Clipboard, please refer to the accompanying source code, very simple. The example code scratches the screen at intervals and saves the image sequence to animation.
DirectX method
Using DREICTX to hold the screen is also very simple, DirectX provides a very elegant implementation.
Each DirectX program contains a memory area that we call Buffering, which holds the video memory content associated with the program, which is called back buffer in the program, and some programs have more than one background buffer. There is also a buffer that, by default, can be accessed by each program-the foreground buffer. The foreground buffer preserves the video memory content associated with the desktop, which is essentially a screen image.
Our program can capture the contents of the current screen by accessing the foreground buffer. Guaranteed by the underlying optimization mechanism of DirectX, our screen-grabbing efficiency is high, at least higher than the GDI method.
Accessing the foreground buffer in the DirectX program is simple, and the IDirect3DDevice8 interface provides a Getfrontbuffer () method that receives a IDirect3DSurface8 object pointer as a parameter, and copy the contents of the foreground buffer to the surface. Idirect3dsurfce8 objects can be obtained using idirect3ddevice8::createimagesurface (). Once the screen content has been saved to this surface, we can use the D3dxsavesurfacetofile () method to save the content directly to the disk BMP file. The sample code is as follows:
extern idirect3ddevice8* g_pd3ddevice;
Void Capturescreen ()
{
IDirect3DSurface8 * PSURFACE;
G_pd3ddeviceàcreateimagesurface (Screenwidth,screenheight,
D3dfmt_a8r8g8b8,&psurface);
G_pd3ddevice->getfrontbuffer (psurface);
D3dxsavesurfacetofile ("Desktop.bmp", D3dxiff_bmp,psurface,
Null,null);
Psurface->release ();
}
Above, G_pd3ddevice is an initialized Idirect3ddevice object that directly saves captured images to a file. However, sometimes we want to access each bit of the image directly, we can use Idirect3dsurface8::lockrect (), which gives us a pointer to the surface memory, which is the captured image data. We can manipulate it by copying the data into the program-defined memory. Look at the following code:
extern void* pbits;
extern idirect3ddevice8* g_pd3ddevice;
IDirect3DSurface8 * PSURFACE;
G_pd3ddeviceàcreateimagesurface (Screenwidth,screenheight,
D3dfmt_a8r8g8b8,&psurface);
G_pd3ddevice->getfrontbuffer (psurface);
D3dlocked_rect Lockedrect;
Psurfaceàlockrect (&lockedrect,null,
d3dlock_no_dirty_update| d3dlock_nosyslock|
d3dlock_readonly)));
for (int i=0 i < screenheight; i++)
{
memcpy ((byte*) pbits + i * screenwidth * BITSPERPIXEL/8,
(byte*) Lockedrect.pbits + i* Lockedrect.pitch,