【问题标题】:How to get Pixel data \ Pixel buffer from a window and extract RGB?如何从窗口获取像素数据\像素缓冲区并提取RGB?
【发布时间】:2012-11-11 20:28:15
【问题描述】:

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

【问题讨论】:

    标签: c++ rgb pixel


    【解决方案1】:

    有两种选择:

    首先,您可以使用 GetPixel()。我用了很多。它工作正常:

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

    在我们的时代,处理器甚至可以在某些情况下使用此函数来处理矩形。

    其次,您可以将屏幕内容复制到位图中。之后,您可以将其放在剪贴板中,使用您的代码进行处理等。那里的核心功能是:

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

    如果需要,我可以发布更详细的 sn-p。

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

    【讨论】:

    • 嘿,谢谢你的回复,是的,你能发布更详细的sn-p吗?这对我来说真的很新鲜......
    • Kirill Kobelev,运行代码后我进入 hMemDC 元素:错误:无法评估表达式。我想我的尺寸参数有问题,margins_rect.left,margins_rect.top 从哪里得到这些?当我创建 hBitamp 时,它假设是 hDC 的大小还是可以更小?如何评估屏幕尺寸?谢谢,
    • 请不要从字面上理解这段代码。它只是显示了函数调用的正确顺序。查看 MSDN 中每个函数的参数描述并进行相应更新。我使用了这段代码,它可以工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 2011-08-29
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    相关资源
    最近更新 更多