嗨即時嘗試捕獲截圖,但圖像出來損壞,任何人都可以看到什麼錯誤的代碼,基本上即時試圖使用createdibsection,所以我可以直接訪問位。Bitblt,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);
+1。如果出現這種情況,文件大小應與預計的像素數不同,因此很容易檢查。 – 2012-07-14 15:42:16
不要忘記計算標題字節。 – 2012-07-14 15:45:04
謝謝,那是錯誤,現在完美運作 – 2012-07-14 16:32:37