Results 1 to 11 of 11

Thread: [RESOLVED] help: find a pixel on screen by its color

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    31

    Resolved [RESOLVED] help: find a pixel on screen by its color

    ok imagine that my entire screen was blue and one pixel was red. how can i find this pixel?

    is there a way to scan through every pixel then when the pixel u want is found , store its location in a variable?



    thanks

  2. #2
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: help: find a pixel on screen by its color

    Take a look at http://www.allapi.net/apilist/apilist.php for GetPixel/GetWindowDC/GetDesktopWindow/GetDIBits/BitBlt APIs.

    Easiest (but very slow) way is using the GetPixel API.
    Something like this:
    VB Code:
    1. lColor = GetPixel(GetWindowDC(GetDesktopWindow()), x, y )

    Or, you can do it by modifying the code from GetDIBits's example. It will be a LOT faster.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    31

    Re: help: find a pixel on screen by its color

    this works for entire screen and not just restricted to the dymentions of program form?

  4. #4
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: help: find a pixel on screen by its color

    Both codes should work for entire screen.

    Do you want it for a particular form only ?
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    31

    Re: help: find a pixel on screen by its color

    nope


    thanks for help and links. ill check em' out.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    31

    Re: help: find a pixel on screen by its color

    when i run the below code. the returned color is 16382453
    however i dont know what that means

    i tried spliting it up into RGB but that didtn work cas it went over 255

    163 824 53
    163 82 453
    16 382 453



    so what does 16382453 mean?

    thx


    VB Code:
    1. Private Declare Function GetDesktopWindow Lib "user32" () As Long
    2. Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    3. Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    4.  
    5. Private Sub Form_Load()
    6.   Dim dhwnd As Long
    7.   Dim dhdc As Long
    8.   Dim color As Long
    9.   Dim x As Long
    10.   Dim y As Long
    11.  
    12.   dhwnd = GetDesktopWindow
    13.   dhdc = GetWindowDC(dhwnd)
    14.  
    15.   color = GetPixel(dhdc, 0, 0)
    16.   Debug.Print color
    17.  
    18. End Sub
    Last edited by Cipherman; Aug 26th, 2006 at 03:29 AM.

  7. #7
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: help: find a pixel on screen by its color

    Quote Originally Posted by Cipherman
    ive often seen the hDC thing in api calls but not entirely sure what it means.

    in this example

    lColor = GetPixel(GetWindowDC(GetDesktopWindow()), x, y )

    does it mean it will only search pixels on the desktop

    -

    and in this example

    ColorMe = GetPixel(hDC, X, Y)

    does it mean it will search pixels on the current screen?
    or do i have to change the hDC to something else to do that?
    Quote Originally Posted by MSDN
    hDc: This property is a Windows operating environment device context handle. The Windows operating environment manages the system display by assigning a device context for the Printer object and for each form and PictureBox control in your application. You can use the hDC property to refer to the handle for an object's device context. This provides a value to pass to Windows API calls.
    You can think it as a drawing board. If your window/control/printer has an hDc, whatever you 'draw' on its hDc, it appears on the screen/printed page.

    Note: Some controls like Label or Image controls don't have any DC. They are painted direcly on the form. They are called lightweight controls.
    Again, some controls, like the Frame control, don't expose the hDC property in VB IDE. You'll not see it in the intellisence list or in the object browser. But you can access it using the GetDC API.


    GetPixel gets the specified pixel of a given hDc (in other words, a particular pixel of a window)

    For your own form/Picturebox etc, you can get the hDc from the .hDc property.
    For getting the hDc of an external window we have 2 APIs:
    Quote Originally Posted by MSDN
    GetDC: The GetDC function retrieves a handle to a display device context (DC) for the client area of a specified window or for the entire screen. You can use the returned handle in subsequent GDI functions to draw in the DC.
    And,
    Quote Originally Posted by MSDN
    GetWindowDC: The GetWindowDC function retrieves the device context (DC) for the entire window, including title bar, menus, and scroll bars. A window device context permits painting anywhere in a window, because the origin of the device context is the upper-left corner of the window instead of the client area.
    You can find their full documentation in your MSDN CD or here

    The desktop itself is a window. It has valid hWnd (Window Handel) and valid DC.
    So, in order to get a particular pixel from it's DC, we need to first get its hWnd by using the GetDesktopWindow API.
    Then we need to get its dc using the GetWindowDC API.
    Finally, we need to get the pixel's color value using the GetPixel API.

    If we break the code,
    lColor = GetPixel(GetWindowDC(GetDesktopWindow()), x, y )
    it will be like,
    VB Code:
    1. Dim lDesktopHwnd As Long
    2. Dim lDesktopDc As Long
    3. Dim lColor As Long
    4.  
    5. lDesktopHwnd = GetDesktopWindow() 'get hWnd of the desktop
    6. lDesktopDc = GetWindowDC(lDesktopHwnd) 'get the hDc of the desktop
    7. lColor = GetPixel(lDesktopDc, x, y) 'get the color of specified pixel
    Hope it helps.

    Edit: Hey ! You changed the complete post.
    Last edited by iPrank; Aug 26th, 2006 at 03:52 AM.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  8. #8
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: help: find a pixel on screen by its color

    Quote Originally Posted by Cipherman
    when i run the below code. the returned color is 16382453
    however i dont know what that means
    i tried spliting it up into RGB but that didtn work cas it went over 255
    163 824 53
    163 82 453
    16 382 453
    so what does 16382453 mean?
    thx
    colors in Windows (or VB) is represented by Long integers (32bit).
    The value 16382453 (or F9F9F5 in Hex) is the value of this color.

    After the debug.print line add,
    Me.BackColor = color
    it will change the form's background color to the color of the specified pixel.

    For VB's long to RGB conversion, you'll need to extract each color from the long value.
    There are many example on the web: http://www.google.com/search?client=...utf-8&oe=utf-8
    Last edited by iPrank; Aug 26th, 2006 at 04:14 AM.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  9. #9

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    31

    Re: help: find a pixel on screen by its color

    many thanks to both of you. im glad someone got to my first question before i edited it


  10. #10
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: help: find a pixel on screen by its color

    Quote Originally Posted by Cipherman
    many thanks to both of you. im glad someone got to my first question before i edited it

    Both of me are glad to help.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  11. #11

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    31

    Re: help: find a pixel on screen by its color

    >.<

    i got some code which worked for me


    VB Code:
    1. Public Sub LongToRGB(ByVal lngColor As Long, intRed As Integer, intGreen As Integer, intBlue As Integer)
    2.  
    3.     intRed = lngColor Mod &H100
    4.     lngColor = lngColor \ &H100
    5.     intGreen = lngColor Mod &H100
    6.     lngColor = lngColor \ &H100
    7.     intBlue = lngColor Mod &H100
    8. End Sub

    many thank iPrank

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width