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

可紊 提交于 2019-12-11 01:17:02

问题


I'm drawing text (textOut) and Rectangles on my window... and I would like to get the RGB buffer from it... How can I do it?


回答1:


There are 2 options:

First, you can use 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.

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


来源:https://stackoverflow.com/questions/13335113/how-to-get-pixel-data-pixel-buffer-from-a-window-and-extract-rgb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!