如何将屏幕图像放入内存缓冲区?

问题描述:

我一直在寻找一种纯粹的WIN32方式将屏幕的图像数据放入缓冲区,以便稍后分析图像中的对象......可悲的是,我还没有找到任何内容。我现在愿意接受可以进行打印屏幕的库/类的建议,但我仍然可以访问它的内存缓冲区。

I've been searching for a pure WIN32 way of getting the image data of a screen into a buffer, for analysis of the objects in the image later... Sadly I haven't found any. I'm now open to suggestions of libraries / classes that could do the "printing of a screen", but I should still have access to it's memory buffer.

任何帮助赞。

编辑:我忘了提到我将继续捕捉屏幕,因此操作速度非常重要。也许有人知道一个好的库?

I forgot to mention that I will be capturing the screen continuously, so the operation speed is very important. Maybe someone knows a good library for this?

// 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);