Using BitBlt to capture desktop pixel colors

心不动则不痛 提交于 2019-12-25 04:46:05

问题


Right now I'm using GetPixel() to retrieve about 64 pixels from the desktop in order to obtain their color. I read about GetPixel() being slow but didn't think it would matter for a few pixels but it's taking like 1.5 seconds each time I run the routine. After doing some research I've concluded that bitblt seems like what I'm looking for. What I want to do is grab a defined area of the desktop (including all windows) and then grab pixel colors at given offsets. Here's what I'm doing now:

     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;

}

I've found a decent amount of tutorial and documentation, but being so new to the windows API they aren't doing to much for me. Thanks!


回答1:


You're probably wanting:

  • CreateCompatibleDC
  • CreateCompatibleBitmap
  • SelectObject, saving the original bitmap
  • BitBlt
  • GetDIBits
  • SelectObject, putting the original bitmap back
  • DeleteBitmap
  • DeleteDC

If you're doing this periodically, then you should do the first three steps only once, repeat BitBlt and GetDIBits, and the last three when your program finishes.



来源:https://stackoverflow.com/questions/4555279/using-bitblt-to-capture-desktop-pixel-colors

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