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

标签 c++ image winapi screen

我一直在寻找一种纯 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/

相关文章:

android - 图像未使用 Html.fromhtml 在 TextView 中显示

python - 从弹出菜单中选择项目 win32 api Python

C 编程 WM_PAINT 会导致不良行为

c++ - 基于非密度的数据聚类算法

ios - 在地板图像上显示基于 iBeacon 的位置

c++ - 尝试使用 POST 将数据添加到我的 MySQL 数据库

jquery - 如何预加载图像以便稍后使用 jQuery 将其设置为背景图像?

windows - 缺少注册表 HKLM\SOFTWARE\Microsoft\VisualStudio\SxS\VS7

c++ - 无法使用 CImg 获得与 mandelbrot 集一起使用的颜色平滑度

C++检查一个对象是否存在于两个映射中