#include <iostream> #include <Windows.h> #include <GdiPlus.h> using namespace std; #pragma comment(lib, "gdiplus") int GetEncoderClsid(const TCHAR *format, CLSID *pClsid) { //using namespace Gdiplus; UINT num = 0; // number of image encoders UINT size = 0; // size of the image encoder array in bytes Gdiplus::ImageCodecInfo *pImageCodecInfo = NULL; Gdiplus::GetImageEncodersSize(&num, &size); if (size == 0) return -1; pImageCodecInfo = (Gdiplus::ImageCodecInfo *)(malloc(size)); if (pImageCodecInfo == NULL) return -1; Gdiplus::GetImageEncoders(num, size, pImageCodecInfo); for (UINT j = 0; j < num; ++j) { if (_tcscmp(pImageCodecInfo[j].MimeType, format) == 0) { *pClsid = pImageCodecInfo[j].Clsid; free(pImageCodecInfo); return j; } } free(pImageCodecInfo); return 0; } void CaptureScreenByGDIPlus() { Gdiplus::GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); { HDC scrdc = ::GetDC(NULL); int nHeight = GetSystemMetrics(SM_CYSCREEN); int nWidth = GetSystemMetrics(SM_CXSCREEN); HDC memdc = CreateCompatibleDC(scrdc); HBITMAP membit = CreateCompatibleBitmap(scrdc, nWidth, nHeight); HBITMAP hOldBitmap = (HBITMAP)SelectObject(memdc, membit); BitBlt(memdc, 0, 0, nWidth, nHeight, scrdc, 0, 0, SRCCOPY); Gdiplus::Bitmap bitmap(membit, NULL); CLSID clsid; GetEncoderClsid(_T("image/jpeg"), &clsid); bitmap.Save(_T("D:\\CaptureScreenByGDIPlus.jpg"), &clsid); SelectObject(memdc, hOldBitmap); DeleteObject(memdc); DeleteObject(membit); ReleaseDC(NULL, scrdc); } Gdiplus::GdiplusShutdown(gdiplusToken); }