CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2001
    Posts
    6

    To get pixel value from images

    If I click on any point in an image that is loaded on a form, is it possible to find what color I am clicking on ? Please help.


  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: To get pixel value from images

    Don't know if this will help but add a Commabd Button, a image control and a picturebox to a form. Add this code and try clicking. See the Point Method in MSDN Help for details on the RGB breakdown.

    option Explicit

    private Sub Command1_Click()
    Image1.Picture = LoadPicture("C:\vb Stuff\Cursors and Bitmaps\Bitmaps\Motleyfool.bmp")
    Picture1.Picture = LoadPicture("C:\vb Stuff\Cursors and Bitmaps\Bitmaps\Motleyfool.bmp")
    End Sub

    private Sub Image1_MouseDown(Button as Integer, Shift as Integer, X as Single, Y as Single)
    Debug.print Hex(Point(X, Y))
    End Sub

    private Sub Picture1_MouseDown(Button as Integer, Shift as Integer, X as Single, Y as Single)
    Debug.print Hex(Point(X, Y))

    End Sub




    John G

  3. #3
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: To get pixel value from images

    It's not possible with an ImageBox, well, it is, if the autoredraw of your form is false, and the point under consideration is not obscured by anything else. And there's a catch, you get the pixel value not as a point on the image, but as one on the form.

    Use a picturebox in such applications. It is much straightforward. And you can use the Point method as John suggested, instead of the GetPixel API, if you're using a picturebox.

    The following shows both


    option Explicit

    private Declare Function GetPixel Lib "gdi32" (byval hdc as Long, byval x as Long, byval y as Long) as Long

    private Sub Image1_MouseDown(Button as Integer, Shift as Integer, x as Single, y as Single)
    Dim R as Byte, G as Byte, B as Byte, Col as Long

    Col = GetPixel(Form1.hdc, (x + Image1.Left) \ Screen.TwipsPerPixelX, (y + Image1.Top) \ Screen.TwipsPerPixelY)
    Label1.BackColor = Col
    Call SepComps(Col, R, G, B)
    Label2.Caption = "R: " & R & " G: " & G & " B: " & B
    End Sub

    private Sub SepComps(byval Col as Long, R as Byte, G as Byte, B as Byte)
    R = Col And &HFF&
    G = (Col And &HFF00&) / &H100&
    B = (Col And &HFF0000) / &H10000
    End Sub

    private Sub Picture1_MouseDown(Button as Integer, Shift as Integer, x as Single, y as Single)
    Dim R as Byte, G as Byte, B as Byte, Col as Long
    Col = GetPixel(Picture1.hdc, x \ Screen.TwipsPerPixelX, y \ Screen.TwipsPerPixelY)
    Label1.BackColor = Col
    Call SepComps(Col, R, G, B)
    Label2.Caption = "R: " & R & " G: " & G & " B: " & B
    End Sub





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

Featured