简体   繁体   English

如何从窗口获取像素数据\\像素缓冲区并提取RGB?

[英]How to get Pixel data \ Pixel buffer from a window and extract RGB?

我正在我的窗口上绘制文本 (textOut) 和矩形……我想从中获取 RGB 缓冲区……我该怎么做?

There are 2 options:有2个选项:

First, you can use GetPixel().首先,您可以使用 GetPixel()。 I used it a lot.我用了很多。 It works fine:它工作正常:

COLORREF GetPixel(
  HDC hdc, 
  int nXPos, 
  int nYPos
); 

With our days processors picking up even a rect using this function may work in certain cases.在我们的日子里,即使是使用此功能的 rect 处理器也可能在某些情况下起作用。

Second, you can copy contents of the screen into a bitmap.其次,您可以将屏幕内容复制到位图。 After that you can place it in clipboard, process with your code, etc. The core function there is:之后,您可以将其放入剪贴板,使用您的代码进行处理等。那里的核心功能是:

BOOL BitBlt(
  _In_  HDC hdcDest,
  _In_  int nXDest,
  _In_  int nYDest,
  _In_  int nWidth,
  _In_  int nHeight,
  _In_  HDC hdcSrc,
  _In_  int nXSrc,
  _In_  int nYSrc,
  _In_  DWORD dwRop
);

I can post more detailed snippet if this is needed.如果需要,我可以发布更详细的片段。

// Pick up the DC.
HDC hDC = ::GetDC(m_control);

// Pick up the second DC.
HDC hDCMem = ::CreateCompatibleDC(hDC);

// Create the in memory bitmap.
HBITMAP hBitmap = ::CreateCompatibleBitmap(hDC, bmp_size_x, bmp_size_y);

// Put bitmat into the memory DC. This will make it functional.
HBITMAP hBmpOld = (HBITMAP)::SelectObject(hDCMem, hBitmap);

// Clear the background.
HBRUSH hBkgr = ::CreateSolidBrush(props.bkgr_brush);
RECT bitmap_rect = { 0, 0, bmp_size_x, bmp_size_y };
::FillRect(hDCMem, &bitmap_rect, hBkgr);
::DeleteObject(hBkgr);

// Do the job.
::BitBlt(hDCMem, margins_rect.left, margins_rect.top,
    size_to_copy_x, size_to_copy_y, hDC,
    screen_from_x, screen_from_y, SRCCOPY);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM