Here is the function:
If you'd prefer the C style:
Brief explanation. The function receives 3 parameters:
1. The device context handle from which the image should be grabbed.
2. The image rectangle.
3. The file name to save the grabbed image.
Capture function creates new DIB section (32-bits but it works with 24 also) and copy the image from the input device context onto this DIB section. The next task is to save this DIB section. In the Win32 application for me was easier to save it as BMP-file. The file structure is simple:
1. File header that contains general information about the file such as the type and size of the image, and the offset to the bit-array of the graphical data. A
2. Bitmap information header contains the information about the image such as width and height, bits per pixel and a compression.
3. The bit-array or the color table - the image pixels.
Note. If your application supports ATL/MFC, you can use CImage class whenever you need to save an image. This MSDN example saves an image in BMP, JPG and PNG formats.
The following code shows how to use this function:
Compiled this application will grab the top-left corner of the desktop.
Of course, when I almost finished my capturing function I found an article in MSDN with an unexpected name "Capturing an Image": http://msdn.microsoft.com/