#include <atlimage.h>
void SavePhoto(LPCTSTR str)
{
HDC hdcSrc = GetDC(NULL);
int nBitPerPixel = GetDeviceCaps(hdcSrc, BITSPIXEL);
int nWidth = GetDeviceCaps(hdcSrc, HORZRES);
int nHeight = GetDeviceCaps(hdcSrc, VERTRES);
CImage image;
image.Create(nWidth, nHeight, nBitPerPixel);
BitBlt(image.GetDC(), 0, 0, nWidth, nHeight, hdcSrc, 0, 0, SRCCOPY);
ReleaseDC(NULL, hdcSrc);
image.Save(str, Gdiplus::ImageFormatPNG);//ImageFormatJPEG
image.ReleaseDC();
}
//调用
const char *str = "D:\\photo.png";
SavePhoto((LPCTSTR)(CString)str);
//注意:HDC hdcSrc = GetDC(NULL); 和 ReleaseDC(NULL, hdcSrc);语句中的NULL是替代参数,在此位置应根据需要提供要截屏的窗口DC。