Thread: GetPixel Function

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    110

    GetPixel Function

    Hello all, I wrote a function to find a pixel on my bitmap then draw a 10x10 square on the location it finds it.


    I first paint the bitmap onto my window, then scan a specific location TLX(top left x) and BRY(bottom right Y) for the collor

    dark red (0xCC0000)
    (ALSO! how do I incriment a hex value? so I could go from 110000 to FF0000)

    if it doesnt find it, it puts a 20x20 square on the screen.

    If you see something wrong with my function or something that should be changed plz tell me.

    Thankyou!
    Code:
    void SearchArea(int TLX,int TLY,int BRX,int BRY)
    {
    	int X;
    	int Y;
    COLORREF clr;
    COLORREF clr2;
    HDC hdcScrn;
    POINT pt;
    pt.x = 0;
    clr2 = 0xCC0000;
    hdcScrn=CreateDC("DISPLAY",0,0,0);
    
    for(Y = 0; Y <= BRY ;Y++)
    {
    	for(X = 0; X<= BRX ; X++)
    	{
         clr=GetPixel(hdcScrn,TLX+X,TLY+Y);
    		if(clr == clr2)
    		{
    		 pt.x = TLX+X;
    		 pt.y = TLY+Y;
    		}
    	}
    if(pt.x != 0)
    {
     BitBlt(bmoMap,
               pt.x,
               pt.y,
               10,
               10,
               bmoStar,
               0,
               0,
               SRCPAINT);
    }
    else 
    {
    BitBlt(bmoMap,
               400,
               400,
               20,
               20,
               bmoStar,
               0,
               0,
               SRCPAINT);
    
    }
    DrawMap();
    }
    Edit: The problem is it doesnt find it

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Run the code in the debugger and find out what values the function is returning.
    Are you sure the colour is in 0xRRGGBB format?

    > so I could go from 110000 to FF0000)
    for ( i = 0x110000 ; i <= 0xFF0000 ; i += 0x10000 )

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by MSDN_COLORREF
    When specifying an explicit RGB color, the COLORREF value has the following hexadecimal form:

    0x00bbggrr
    The low-order byte contains a value for the relative intensity of red; the second byte contains a value for green; and the third byte contains a value for blue. The high-order byte must be zero. The maximum value for a single byte is 0xFF.

    To create a COLORREF color value, use the RGB macro. To extract the individual values for the red, green, and blue components of a color value, use the GetRValue, GetGValue, and GetBValue macros, respectively
    Code:
    clr2 = RGB(0xCC, 0x00, 0x00);

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    With your guys help I have writen what I needed to so far, it sets the mouse to the location to the color, but I just realized something, its off about X,Y the reason is

    CreateDC("DISPLAY",0,0,0) which takes in the Display, so whatever is ontop (which will be great for my screenshot function).

    BUt I only want to look inside my window im using.

    Does anyone know the correct parameters to look inside the form window?

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Just look inside the rectange returned by GetWindowRect() for your window.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    GetWindowRect() returns a booL?

    All I want to do is use GetPixel regardless of what window is ontop, and only search inside the window its in.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The second argument returns a RECT.
    Code:
    /* Retrieve the pixels making up a window using GetPixel. */
    RECT rc;
    GetWindowRect(hwnd, &rc);
    
    for (int x = rc.left;x < rc.right;x++)
    {
        for (int y = rc.top;y < rc.bottom;y++)
        {
            clr = GetPixel(hdcScreen, x, y);
            ...
        }
    }
    On a side note, GetPixel is not the most efficient method of retrieving a block of pixels.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Yeah I tottaly agree.

    My problem is the hdcScreen. It seems to target whatever is ontop, I wont it to focus on the programs screen and GetPixel from its own screen regardless what is ontop.


    How do I convert the current screens handle (not the one ontop) to an HDC

    So GetPixel will only getpixels from its program screen even if there is another program running ontop.

    Thnx

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Use GetDC(), pass the HWND of your dialog as a param.
    I would get the required DC outside the function and send the HDC as a param.

    Don't forget to ReleaseDC() if you 'get' or DeleteDC() if you create one.

    This will only get the client area, not menu ect.

    >>Hello all, I wrote a function to find a pixel on my bitmap then draw a 10x10 square on the location it finds it.

    Why use the screen DC when you seem to have two globals, bmoMap and bmoStar?

    Coords should be pt.x-5, pt.y-5, 10, 10 (but you got that didn't you?)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Code:
    void SearchArea(int TLX,int TLY,int BRX,int BRY,HWND hwnd)
    {
    	int X;
    	int Y;
    	int i=0;
    	int G;
    	int B;
    	int R = 0;
    	bool found;
    	COLORREF clr;
    	COLORREF Database[1000];
    
    	HDC hdcScrn;
    	POINT pt;
    	pt.x = 0;
    	pt.y = 0;
    
    for(B=0;B<=11;B++)
    {
    	for(G=0;G<=25;G++)
    	{
            Database[R] = RGB(103, 101+B, 125+G);
    		R++;
    	}
    }
    
    	hdcScrn=GetDC(hwnd);
    
    	
       found = false;
    	for(Y = 0; Y <= (BRY-TLY) ;Y++)
    	{
    		for(X = 0; X<= (BRX-TLX) ; X++)
    		{
    		clr=GetPixel(hdcScrn,TLX+X,TLY+Y);
    	
    		for(i=0;i<=312;i++)
    		{
    		    if(clr == Database[i])
    			{
    
               found = true;
    		 pt.x = TLX+X+8;
    		 pt.y = TLY+Y+30;
    			}//end IF
    		}//end IF ColorSearch
    			if(found)
    				break;
    		}//end X
    		if(found)
    			break;
    
    	}//end Y
    		
    if(found)
    {
    
    
    	SetCursorPos(pt.x+10,pt.y+30);
    }
    
    		if(!found) 
    		{
    	//Do Nothing yet
    
    		}
    	DrawMap();
    	DeleteDC(hdcScrn);
    
    }

    See, the operations work the mouse placement works, the problem is if another window is ontop it doesnt work.

    The Database of colors is the colors its looking for, and whatever it finds first then its set.

    So agian, Im not sure how to fix this.

    This is a macro program, that takes a screenshot of the top window (works) then in the background blits the image in my prog and searches it for the database of colors and moves the mouse to the location.

    The screenshot works, the finding the location works, it just not only searching inside my prog, it searches whatever is ontop.

    Is there a way I can search through a bitmap thats not ontop for a specific pixel?

    Sorry I havnt been so clear.

  11. #11
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I'm still not quite sure what you're asking. If you already have a screen shot of the target window, you can search that. Just store it in a memory DC/bitmap and search it. It sounds like you are blitting directly from the screen to your window and not storing the screen shot in a memory bitmap. Is this the case? Maybe you could post the screen shot code.

    Or are you asking how to take a screen shot of window that is not on top? Getting a screen shot of a window that is not on top is not simple. Have a look at this article.
    Window Contents Capturing using WM_PRINT Message

  12. #12
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Your right,

    How would I go about storing the screenshot into memory then searching through the pixels in memory?

    Code:
      HDC hdcDesk;
      hdcDesk=GetDC(GetDesktopWindow());
      BitBlt(bmoMap, 0, 0, 1024,768,hdcDesk,0,0,SRCCOPY);
    ReleaseDC(HWND_DESKTOP,hdcDesk);

  13. #13
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    	// Set up memory dc and bitmap to contain our screen shot (size is cx by cy)...
    	HDC hdcScreenShot = CreateCompatibleDC(hdcScreen);
    	HBITMAP hBmpScreenShot = CreateCompatibleBitmap(hdcScreen, cx, cy);
    	HBITMAP hOldBmpScreenShot = (HBITMAP) SelectObject(hdcScreenShot, hBmpScreenShot);
    
    	// Copy screen into our memory bitmap...
    	BitBlt(hdcScreenShot, 0, 0, cx, cy, hdcScreen, 0, 0, SRCCOPY);
    
    	// Use memory bitmap/DC as needed (including GetPixel, bitblt to window, etc)...
    
    	// Cleanup (probably in WM_DESTROY)...
    	SelectObject(hdcScreenShot, hOldBmpScreenShot);
    	DeleteObject(hBmpScreenShot);
    	DeleteDC(hdcScreenShot);
    You may want to consider only copying the target window if that is all you need for improved efficiency.

  14. #14
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Wow man, I really want to thank you for helping me with this, it trully means a lot.

    Got it fully working, infact this execellent way of doing it cuts down my application process time by 1000% (really it does)

    Thnxs to all who helped

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM