Results 1 to 4 of 4

Thread: Color in a picture??

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    273

    Talking Color in a picture??

    How can i get the color value from a picture (in pixel 14*54 for exemple) . Give me something please api, codesnippet anything.
    Carl Odin

  2. #2
    AIS_DK
    Guest
    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:
    1. 'Code to retrieve color
    2. Public Const CLR_INVALID = &HFFFF
    3. Public Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    4.  
    5. public function getColor(byval hDC as long, byval X as long, byval Y as long) as long
    6. lngColor = GetPixel(Hdc,X, Y)
    7. if lngColor = CLR_INVALID then
    8.    Err.raise vbobjecterror + 25, "Coordinates out of boundaries"
    9. else
    10.    getColor = lngColor
    11. end if
    12. 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.

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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:
    VB Code:
    1. Private Const CLR_INVALID As Long = -1
    2. 'or
    3. Private Const CLR_INVALID = &HFFFFFFFF
    Best regards

  4. #4
    Megatron
    Guest
    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.

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