Results 1 to 8 of 8

Thread: Getting pixel color?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    157

    Getting pixel color?

    Hello, I was wondering is it possible from a VB program to grab the color of a pixel at a given location(Like.. 20x 40y)? And if so... How?

  2. #2
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Getting pixel color?

    You mean this?

    VB Code:
    1. Private Declare Function GetPixel Lib "gdi32" (ByVal Hdc As Long, ByVal x As Long, ByVal y As Long) As Long

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    157

    Re: Getting pixel color?

    How exactly would I use it? Because when I try it returns -1?... What I am trying to do is get a color value from a web page, then press a key/click something if the color is there.

  4. #4
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729
    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
    Addicted Member
    Join Date
    Mar 2005
    Posts
    157

    Re: Getting pixel color?

    Thank you a lot.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    157

    Re: Getting pixel color?

    Sorry for the double post, but how would I make it so when I clicked the left mouse button it would output the color, mouse x and mouse y?

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

    Re: Getting pixel color?

    Try this:
    VB Code:
    1. ' Add a commandbutton in your form and paste this code.
    2. ' set the scalemode of the form to Pixels
    3. ' run the project, click the command button then click anywhere on the screen
    4. Option Explicit
    5.  
    6. Private Type POINTAPI
    7.     X As Long
    8.     Y As Long
    9. End Type
    10.  
    11. Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
    12. Private Declare Function ReleaseCapture Lib "user32" () As Long
    13. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    14. Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
    15.  
    16. Dim pt As POINTAPI
    17.  
    18. Private Sub Command1_Click()
    19.     SetCapture Me.hwnd
    20.     Command1.Enabled = False
    21. End Sub
    22.  
    23. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    24.     If Button = vbLeftButton Then
    25.         pt.X = X
    26.         pt.Y = Y
    27.         ClientToScreen Me.hwnd, pt
    28.         ReleaseCapture
    29.         MsgBox "Clicked at " & pt.X & ":" & pt.Y & vbCrLf & _
    30.            "Get the color of pixel here"
    31.         Me.Caption = "Form1"
    32.         Command1.Enabled = True
    33.     End If
    34. End Sub
    35.  
    36. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    37.     If Not Command1.Enabled Then
    38.     ' this code is not running sometimes when mouse is over taskbar
    39.         pt.X = X
    40.         pt.Y = Y
    41.         ClientToScreen Me.hwnd, pt
    42.         Me.Caption = pt.X & ":" & pt.Y
    43.     End If
    44. End Sub
    Download APIGuide and API Viewer from the links in my sig. In APIGuide you'll find many other mouse-related APIs and their description.
    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

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    157

    Re: Getting pixel color?

    Works great thanks.

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