使用BitBlt捕获桌面像素颜色

时间:2010-12-29 15:19:20

标签: c winapi bitblt

现在我正在使用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来说是如此新鲜,他们并没有为我做太多。谢谢!

1 个答案:

答案 0 :(得分:3)

你可能想要:

  • CreateCompatibleDC
  • CreateCompatibleBitmap
  • SelectObject,保存原始位图
  • 的BitBlt
  • 的GetDIBits
  • SelectObject,将原始位图重新放回
  • DeleteBitmap
  • DeleteDC

如果您定期执行此操作,那么您应该只执行前三个步骤,重复BitBltGetDIBits,以及程序完成后的最后三个步骤。