Click to See Complete Forum and Search --> : To get pixel value from images


trueblue_romeo
September 2nd, 2001, 03:49 PM
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.

John G Duffy
September 2nd, 2001, 04:38 PM
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

shree
September 2nd, 2001, 09:38 PM
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