嗨,我正在尝试捕获屏幕截图,但图像已损坏,任何人都可以看到代码有什么问题,基本上我正在尝试使用 createdibsection,因此我可以直接访问这些位。
这是生成的图片的样子 http://oi47.tinypic.com/33c4zko.jpg
bool FrameData::Create(int width, int height, ImageFormat imgFormat, HWND sourceWindow)
{
if(width < 0)
return false;
srcWndDC = GetDC(sourceWindow);
hdc = CreateCompatibleDC(srcWndDC);
if(hdc == nullptr || srcWndDC == nullptr)
{
return false;
}
memset(&bmpInfo, 0, sizeof(BITMAPINFO));
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = width;
bmpInfo.bmiHeader.biHeight = -height; // top-down
bmpInfo.bmiHeader.biPlanes = 1;
switch(imgFormat)
{
case ImageFormat::RGB16:
bmpInfo.bmiHeader.biBitCount = 16;
break;
case ImageFormat::RGB24:
bmpInfo.bmiHeader.biBitCount = 24;
break;
case ImageFormat::RGB32:
bmpInfo.bmiHeader.biBitCount = 32;
break;
default:
return false;
}
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biSizeImage = height * width * imgFormat;
hBitmap = CreateDIBSection(srcWndDC, &bmpInfo, DIB_RGB_COLORS, (void**)&bits, NULL, NULL);
if(hBitmap == nullptr)
{
return false;
}
return true;
}
bool FrameData::SaveFrameToFile(std::string& filename)
{
BITMAPFILEHEADER bmpFileHdr = {0};
bmpFileHdr.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage;
bmpFileHdr.bfType='MB';
bmpFileHdr.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
std::ofstream outfile;
outfile.open(filename);
if(!outfile.is_open())
{
return false;
}
outfile.write((char*)&bmpFileHdr, sizeof(BITMAPFILEHEADER));
outfile.write((char*)&bmpInfo.bmiHeader, sizeof(BITMAPINFOHEADER));
outfile.write((char*)bits, bmpInfo.bmiHeader.biSizeImage);
outfile.close();
return true;
}
这就是代码,然后我只是用它来捕获屏幕
SelectObject(data.GetHDC(), data.GetHBitmap());
BitBlt(data.GetHDC(),0,0,data.GetWidth(), data.GetHeight(), data.GetSourceWindowDC(),0,0 , SRCCOPY | CAPTUREBLT);