How can i get the color value from a picture (in pixel 14*54 for exemple) . Give me something please api, codesnippet anything.
Printable View
How can i get the color value from a picture (in pixel 14*54 for exemple) . Give me something please api, codesnippet anything.
Get pixel retrieves the RGB value of the pixel at the (X,Y) coordinates in the current selected DC
Public Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Put your picture into a picture box, and you should be able to retrieve the color of any pixel with this code:
VB Code:
'Code to retrieve color Public Const CLR_INVALID = &HFFFF Public Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long public function getColor(byval hDC as long, byval X as long, byval Y as long) as long lngColor = GetPixel(Hdc,X, Y) if lngColor = CLR_INVALID then Err.raise vbobjecterror + 25, "Coordinates out of boundaries" else getColor = lngColor end if end function
The HDC argument must be the hdc of the picturebox containing the picture (you can get this through: me.picture1.hdc, or whatever your picturebox is called).
Good luck
P.S.
I havn't tested this code, so there might be an error or two, but it should be pretty easy to sort them out.
There is just one small problem with the code AIS_DK provided.
Using his code you can't pick a pixel which is colored yellow ;).
The CLR_INVALID is declared like this:Best regardsVB Code:
Private Const CLR_INVALID As Long = -1 'or Private Const CLR_INVALID = &HFFFFFFFF
If you need to get the colour of pixels from an entire picture, you should use GetPixel. Otherwise, if all you need is to retrieve a point where the mouse is clicked, use VB's Point method.