Screen Capturing via GDI+ and GDI - Persisting the Captured Images via GDI
(Page 4 of 5 )
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 it—for detailed coding you can refer to the source code at the end of the article.
Next: Adding a Reference to GDI and Visualizing the Result >>
More Windows Scripting Articles
More By Xianzhong Zhu