【问题标题】:Access pixels of 24-bit bitmap, without using GetPixel()?访问 24 位位图的像素,而不使用 GetPixel()?
【发布时间】:2013-02-20 09:40:13
【问题描述】:

我目前正在寻找一种方法来访问和测试从目标窗口复制的位图的各个像素,而不使用非常慢的GetPixel() 方法。鉴于memDC 在调用BitBlt() 时包含位图的副本,是否有更快的方法来遍历各个像素并测试它们的值?

HWND target = (HWND)0x0002051A; // this is just for debugging; when i get to the release version it will detect the intended window automatically
HBITMAP hBitmap;
RECT winRect;
HDC winDC, memDC;
winDC = GetDC(target);
GetClientRect(target, &winRect);
memDC = CreateCompatibleDC(winDC);
hBitmap = CreateCompatibleBitmap(winDC, winRect.right-winRect.left, winRect.bottom-winRect.top);
SelectObject(memDC, hBitmap);
BitBlt(memDC, 0, 0, winRect.right-winRect.left, winRect.bottom-winRect.top, winDC, 0, 0, SRCCOPY);
// Perform other tasks based on the color values of pixels...
ReleaseDC(memDC);
ReleaseDC(winDC);
DeleteObject(hBitmap);

【问题讨论】:

    标签: windows winapi colors bitmap gdi


    【解决方案1】:

    如果您使用CreateDIBSection 创建目标位图,您可以访问原始像素,而不必通过GetPixel

    例如如果您传递的 BITMAPINFO 指定了 32-bpp 图像(最简单的解释格式),您可以使用类似 *( ((LPDWORD)pBits) + ( y * width ) + x) 的方式访问任意像素。

    请参阅BITMAPINFOHEADER 的文档以了解不同位深度格式在内存中的布局方式的详细信息,并记住 a) 位图行始终是 DWORD 对齐的,并且 b) 您需要传递负高度来创建顶部-向下位图。

    如果您不使用或无法使用CreateDIBSection,您可以使用GetDIBits 以您想要的DIB 格式获取位图的内存副本。 (在调用GetDIBits之前,请确保从任何DC中选择位图。)

    【讨论】:

      猜你喜欢
      • 2011-07-07
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-12
      • 1970-01-01
      • 2018-04-01
      相关资源
      最近更新 更多