开发者

Using BitBlt to capture desktop pixel colors

开发者 https://www.devze.com 2023-02-01 09:52 出处:网络
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 i

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 开发者_如何学Cwant 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号