我一直在寻找一种纯 WIN32 的方法来将屏幕的图像数据放入缓冲区,以便稍后分析图像中的对象......遗憾的是我没有找到任何方法。我现在愿意接受可以执行“屏幕打印”的库/类的建议,但我仍然应该可以访问它的内存缓冲区。
感谢任何帮助。
编辑:我忘了说我会连续捕获屏幕,所以操作速度非常重要。也许有人知道一个好的图书馆?
最佳答案
// get screen DC and memory DC to copy to
HDC hScreenDC = GetDC(0);
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
// create a DIB to hold the image
BITMAPINFO bmi = { 0 };
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biWidth = GetDeviceCaps(hScreenDC, HORZRES);
bmi.bmiHeader.biHeight = -GetDeviceCaps(hScreenDC, VERTRES);
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
LPVOID pBits;
HBITMAP hBitmap = CreateDIBSection(hMemoryDC, &bmi, DIB_RGB_COLORS, &pBits, NULL, 0);
// select new bitmap into memory DC
HGDIOBJ hOldBitmap = SelectObject(hMemoryDC, hBitmap);
// copy from the screen to memory
BitBlt(hMemoryDC, 0, 0, bmi.bmiHeader.biWidth, -bmi.bmiHeader.biHeight, hScreenDC, 0, 0, SRCCOPY);
// clean up
SelectObject(hMemoryDC, hOldBitmap);
DeleteDC(hMemoryDC);
ReleaseDC(0, hScreenDC);
// the image data is now in pBits in 32-bpp format
// free this when finished using DeleteObject(hBitmap);
关于c++ - 如何将屏幕图像放入内存缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18858638/