捕获桌面屏幕 QT/C++ WinAPI
Capture Desktop Screen QT/C++ WinAPI
我正在使用 QT/C++,我想捕获我的桌面屏幕并获取像素缓冲区。
我正在使用这个函数,但它 returns 奇怪的像素值,有人能告诉我为什么吗?
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);
}
输出是 1 1 1 255 1 1 1 255 ...但是我的屏幕顶部的第一个像素是白色的。应该是 255 255 255 255 等...
谢谢!
已解决!
感谢 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;
我刚刚用这条 bmi.bmiHeader.biHeight = - nScreenHeight;
替换了这一行 bmi.bmiHeader.biHeight = nScreenHeight;
并且我从顶行获取了像素。
它可以帮助其他人:)
我正在使用 QT/C++,我想捕获我的桌面屏幕并获取像素缓冲区。
我正在使用这个函数,但它 returns 奇怪的像素值,有人能告诉我为什么吗?
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);
}
输出是 1 1 1 255 1 1 1 255 ...但是我的屏幕顶部的第一个像素是白色的。应该是 255 255 255 255 等...
谢谢!
已解决! 感谢 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;
我刚刚用这条 bmi.bmiHeader.biHeight = - nScreenHeight;
替换了这一行 bmi.bmiHeader.biHeight = nScreenHeight;
并且我从顶行获取了像素。
它可以帮助其他人:)