1
votes

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
This code doesn't appear to call BitBlt. It's also not clear what your question is. Is the problem with this code, or is it with some code that you've not written yet?David Heffernan
Yea I'm not using BitBlt but would like to. I've been looking though the documentation and can't quite get it. I'd like to replace my GetPixel() routine with BitBlt!Andrew Carter
then write some code, try it out, try yourself to solve the problems until you can get no further. Then post your code here and explain what you have done and why you are stuck.David Heffernan

1 Answers

4
votes

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.