Read pixel color data from screen?

This is a discussion on Read pixel color data from screen? within the Windows Programming forums, part of the Platform Specific Boards category; Hey all, I'm looking for a way to be able to read the color data at say pixel (0,5) on ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    102

    Read pixel color data from screen?

    Hey all, I'm looking for a way to be able to read the color data at say pixel (0,5) on the screen and check it's color. Is there a way to do this that isn't window dependent? and if not, how do you do it? Thanks, John
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    GetPixel() on the DC.

    Not the best idea.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Ok, I tried that, however I need to access the color of a pixel in a separate thread than the one that calls the function. Should I pass in HWND_TOPMOST for the hwnd parameter?
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Give this a try with the code from your other post.

    Code:
    __declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
    {
        MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
        if (pMouseStruct != NULL)
            printf("Mouse position X = %d  Mouse Position Y = %d\n", pMouseStruct->pt.x,pMouseStruct->pt.y);
    
        HWND window = GetForegroundWindow();
        HDC hDC = GetDC(window);
        COLORREF rgb = GetPixel(hDC, pMouseStruct->pt.x, pMouseStruct->pt.y);
        COLORREF red = GetRValue(rgb); 
        COLORREF green = GetGValue(rgb); 
        COLORREF blue = GetBValue(rgb); 
        printf("%02X %02X %02X\n",red,green,blue);
        return CallNextHookEx(hMouseHook,
            nCode,wParam,lParam);
    }

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Actually guys, thanks for the help, but I found out how to get the handle to the window I was targetting anyway. Thanks
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How does this user get screen to run under root?
    By Overworked_PhD in forum Tech Board
    Replies: 2
    Last Post: 06-28-2009, 10:31 AM
  2. confused with read()
    By _EAX in forum Linux Programming
    Replies: 2
    Last Post: 03-17-2008, 05:14 PM
  3. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 01:51 PM
  4. Windows Pixel Blocks... PLEASE READ!
    By minime6696 in forum Windows Programming
    Replies: 20
    Last Post: 01-24-2002, 08:33 PM
  5. Green Pixel On My Screen???
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 01-21-2002, 08:09 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21