【问题标题】:Getting individual pixels of another window with BitBlt使用 BitBlt 获取另一个窗口的单个像素
【发布时间】:2012-01-31 15:40:58
【问题描述】:

这就是我目前正在做的事情:

  • 通过GetWindowDC获取窗口DC
  • CreateCompatibleDC创建一个兼容的DC
  • 在我的兼容 DC 上调用 GetPixel

不幸的是,我所有的 GetPixel 调用都返回 CLR_INVALID。这是我的代码。

bool Gameboard::Refresh()
{
  bool  ret = false;
  HDC   context, localContext;

  context = GetWindowDC(m_window);
  if (context != NULL)
  {
    localContext = CreateCompatibleDC(context);
    if (localContext != NULL)
    {
      if (BitBlt(localContext, 0, 0, GameboardInfo::BoardWidth, GameboardInfo::BoardHeight,
        context, GameboardInfo::TopLeft.x, GameboardInfo::TopLeft.y, SRCCOPY))
      {
        ret = true;
        // several calls to GetPixel which all return CLR_INVALID
      }
      DeleteDC(localContext);
    }
    ReleaseDC(m_window, context);
  }
  return ret;
}

有什么想法吗?

【问题讨论】:

    标签: c++ winapi gdi bitblt getpixel


    【解决方案1】:

    我认为您需要在设备上下文中选择位图。

    “必须在设备上下文中选择位图,否则,所有像素都会返回 CLR_INVALID。” - GetPixel()

    bool Gameboard::Refresh()
    {
      bool  ret = false;
      HDC   context, localContext;
    
    
      HGDIOBJ origHandle;
    
    
      context = GetWindowDC(m_window);
      if (context != NULL)
      {
        localContext = CreateCompatibleDC(context);
    
    
        origHandle = SelectObject(localcontext,CreateCompatibleBitmap(context, GameboardInfo::BoardWidth, GameboardInfo::BoardHeight));
    
    
        if (localContext != NULL)
        {
          if (BitBlt(localContext, 0, 0, GameboardInfo::BoardWidth, GameboardInfo::BoardHeight,
            context, GameboardInfo::TopLeft.x, GameboardInfo::TopLeft.y, SRCCOPY))
          {
            ret = true;
            // several calls to GetPixel which all return CLR_INVALID
          }
    
          SelectObject(localcontext, origHandle);
    
    
          DeleteDC(localContext);
        }
        ReleaseDC(m_window, context);
      }
      return ret;
    }
    

    【讨论】:

    • 我很确定我的 GetPixel 调用中的 x/y 值已经是偏移量而不是绝对位置。
    • 您仍然需要在兼容的 DC 中选择位图,否则 BitBlt 无处可复制像素。另外,使用 GetDIBits -- 它比使用 GetPixel 快得多。
    • @RogerLipscombe 如果我只需要 480x480 正方形中的 64 个像素怎么办? GetDIBits 还是更快吗?
    • 任何扩展的错误信息? Win 32 API 通常不会在不产生错误的情况下挂起,至少在调试器中是这样。
    • 如果你得到超过一个像素的 CreateComaptilbeBitmap(hDC,nWidth,nHieght)。如果您想改用 GetDIBits,请不要使用 BitBlt。 GetDIBits 返回指向位本身的指针,因此您实际上可以自己遍历或检查它们,或者您可以使用 CreateDIBSection 和 StretchDIBits 仅复制您想要检查的位。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多