[英]Capture Desktop Screen QT/C++ WinAPI
I'm using QT/C++ and I would like to capture the screen of my desktop and get the pixels buffer. 我正在使用QT / C ++,我想捕获桌面的屏幕并获取像素缓冲区。
I'm using this function but it returns weird pixels values, can someone tell me why ? 我正在使用此函数,但它返回奇怪的像素值,有人可以告诉我为什么吗?
void CaptureScreen()
{
int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
HWND hDesktopWnd = GetDesktopWindow();
HDC hDesktopDC = GetDC(hDesktopWnd);
HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
HBITMAP hCaptureBitmap = CreateCompatibleBitmap(hDesktopDC, nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC, hCaptureBitmap);
BitBlt(hCaptureDC, 0, 0, nScreenWidth, nScreenHeight, hDesktopDC, 0,0, SRCCOPY|CAPTUREBLT);
BITMAPINFO bmi = {0};
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biWidth = nScreenWidth;
bmi.bmiHeader.biHeight = nScreenHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
unsigned char *pPixels = new unsigned char[nScreenWidth * nScreenHeight * 4];
GetDIBits(hCaptureDC, hCaptureBitmap, 0, nScreenHeight, pPixels, &bmi, DIB_RGB_COLORS);
for (int i = 0; i < 200; i++) {
qDebug() << (int) pPixels[i];
}
delete[] pPixels;
ReleaseDC(hDesktopWnd, hDesktopDC);
DeleteDC(hCaptureDC);
DeleteObject(hCaptureBitmap);
}
Output is 1 1 1 255 1 1 1 255 ... but first pixels on the top of screen I have is white. 输出是1 1 1 255 1 1 1 255 ...,但是我在屏幕顶部的第一个像素是白色。 It should be 255 255 255 255 etc... 它应该是255 255 255 255等。。。
Thank you ! 谢谢 !
Resolved! 解决! Thanks to RbMm comment : 感谢RbMm评论:
A bottom-up DIB is specified by setting the height to a positive number, while a top-down DIB is specified by setting the height to a negative number.
the first line is bottom. if want another order use bmi.bmiHeader.biHeight = -nScreenHeight;
I just replaced this line bmi.bmiHeader.biHeight = nScreenHeight;
我只是替换了这一行bmi.bmiHeader.biHeight = nScreenHeight;
by this bmi.bmiHeader.biHeight = - nScreenHeight;
通过此bmi.bmiHeader.biHeight = - nScreenHeight;
and I get pixels from top line. 我从顶行得到像素。 It can help someone else :) 它可以帮助别人:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.