此函数创建了1080p高清图片,即分辨率为:1920 x 1080
如果想创建不同的分辨率图片,可以修改函数中的 nWidth 和 nHeight.
此文仅供参考,如有不妥之处,请多多指教。
void Create1080p()
{int nWidth = 1920;
int nHeight = 1080;
int size = nWidth*nHeight;
HDC hdc = ::GetDC(GetDesktopWindow());//获取屏幕DC
HDC hdcmem = ::CreateCompatibleDC(hdc);//创建兼容dC
HBITMAP hbitmap = ::CreateCompatibleBitmap(hdc, nWidth, nHeight);
HBITMAP holdmap = (HBITMAP)::SelectObject(hdcmem, hbitmap);
::BitBlt(hdcmem, 0, 0, nWidth, nHeight, hdc, 0, 0, SRCCOPY);
BITMAPINFO* bMapInfo = (BITMAPINFO*)::LocalAlloc(LPTR, sizeof(BITMAPINFO));
bMapInfo->bmiHeader.biBitCount = 24;
bMapInfo->bmiHeader.biClrImportant = 0;
bMapInfo->bmiHeader.biCompression = 0;
bMapInfo->bmiHeader.biHeight = nHeight;
bMapInfo->bmiHeader.biPlanes = 1;
bMapInfo->bmiHeader.biSize = sizeof(BITMAPINFO);
bMapInfo->bmiHeader.biSizeImage = size*3;
bMapInfo->bmiHeader.biWidth = nWidth;
bMapInfo->bmiHeader.biXPelsPerMeter = 0;
bMapInfo->bmiHeader.biYPelsPerMeter = 0;
BITMAPFILEHEADER bmfh;
bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfSize = sizeof(BITMAPFILEHEADER);
bmfh.bfType = 0x4D42;
RGBTRIPLE *rgb = new RGBTRIPLE[size];
ZeroMemory(rgb, sizeof(byte)*size*3);
::GetDIBits(hdc, hbitmap, 0, nHeight, rgb, bMapInfo, DIB_RGB_COLORS);
CString bmpfile = _T("..\\top.bmp");
CFile file(bmpfile, CFile::modeCreate|CFile::modeWrite);
file.Write(&bmfh, sizeof(BITMAPFILEHEADER));
file.Write(&bMapInfo->bmiHeader, sizeof(BITMAPINFOHEADER));
file.Write(rgb, bMapInfo->bmiHeader.biSizeImage);
file.Close();
::LocalFree(bMapInfo);
delete rgb;
::SelectObject(hdcmem, holdmap);
::DeleteObject(hbitmap);
::DeleteDC(hdcmem);
::DeleteObject(hdc);
}