Using BitBlt to capture desktop pixel colors
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.
精彩评论