1

现在我正在使用 GetPixel() 从桌面检索大约 64 个像素以获得它们的颜色。我读到 GetPixel() 很慢,但认为这对几个像素无关紧要,但每次运行例程都需要 1.5 秒。在做了一些研究之后,我得出的结论是 bitblt 似乎是我正在寻找的东西。我想要做的是抓取桌面的定义区域(包括所有窗口),然后在给定的偏移量处抓取像素颜色。这就是我现在正在做的事情:

     for (y=0;y<=7;y++) {
     for (x=0;x<=7;x++) {
     //gameScreen is a struct containing the offset from the top left of the monitor
     //to the area of the screen I need
         grid[y][x]=getColor(gameScreen.x+((x*40)+20),gameScreen.y+((y*40)+20));
         }
     }

int getColor(int x, int y) {
//create new point at given coordinates
POINT point;
point.x=x;
point.y=y;
//convert to logical points
DPtoLP(desktopDC,&point,2);
//get pixel color
//desktopDC is an HDC from GetWindowDC(GetDesktopWindow())
int pixel=GetPixel(desktopDC,point.x,point.y);
return pixel;

}

我找到了大量的教程和文档,但是对于 Windows API 来说太新了,它们对我来说并没有做太多。谢谢!

4

1 回答 1

4

你可能想要:

  • 创建兼容DC
  • 创建兼容位图
  • SelectObject,保存原始位图
  • 比特币
  • 获取DIBits
  • SelectObject,把原来的位图放回去
  • 删除位图
  • 删除DC

如果您定期执行此操作,那么您应该只执行前三个步骤一次,重复BitBltand GetDIBits,最后三个步骤在您的程序完成时执行。

于 2010-12-29T19:58:34.030 回答