Results 1 to 21 of 21

Thread: reading the color of pixels

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    reading the color of pixels

    how would i read the pixels in a picture box and store there colors in a string like

    green,black,blue,pink

    or something like that or how would i read just the first pixel of a picture i can do the rest.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: reading the color of pixels

    You can read a pixel by using the Point method of the picturebox, eg:

    MsgBox Picture1.Point(1,1)


    Note however that this only returns the colour number, it will not return words as you want - you will have to convert the number somehow.

    I think I posted an example of a method to do this, so I'll have a little search!

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: reading the color of pixels

    Posibly by using the GetPixel API:
    The GetPixel function retrieves the red, green, blue (RGB) color value of the pixel at the specified coordinates.

    VB4-32,5,6
    Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long

    VB.NET
    System.Drawing.Bitmap.GetPixel

    Operating Systems Supported

    Requires Windows NT 3.1 or later; Requires Windows 95 or later


    Library

    Gdi32


    Parameter Information

    · hdc
    Identifies the device context.

    · nXPos
    Specifies the logical x-coordinate of the pixel to be examined.

    · nYPos
    Specifies the logical y-coordinate of the pixel to be examined.


    Return Values

    If the function succeeds, the return value is an RGB value. If the pixel is outside of the current clipping region, the return value is CLR_INVALID.

  4. #4
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: reading the color of pixels

    Quote Originally Posted by si_the_geek
    You can read a pixel by using the Point method of the picturebox, eg:

    MsgBox Picture1.Point(1,1)

    Kewl

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: reading the color of pixels

    Quote Originally Posted by si_the_geek
    You can read a pixel by using the Point method of the picturebox, eg:

    MsgBox Picture1.Point(1,1)


    Note however that this only returns the colour number, it will not return words as you want - you will have to convert the number somehow.

    I think I posted an example of a method to do this, so I'll have a little search!
    how would i tell wat color it is?

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: reading the color of pixels


  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: reading the color of pixels

    I'm sure I've done this before, but couldn't find it - so here it is again!

    This works by converting the number you get (a long) into separate R,G,B values, and then 'estimating' what the colour is - as there is such a range of values this is about the best you will be able to do.
    VB Code:
    1. Private Function ColourLongToWords(ByVal lColour As Long) As String
    2. 'Converts an RGB colour number to a string equivalent
    3. 'By Si_the_geek, VBForums
    4.  
    5.     'ensure value is within range for colours
    6.   lColour = lColour And &HFFFFFF
    7.  
    8.     'convert to separate RGB values
    9. Dim iRed As Integer, iGreen As Integer, iBlue As Integer
    10.   iRed = lColour And &HFF
    11.   iGreen = (lColour \ &H100) And &HFF
    12.   iBlue = lColour \ &H10000
    13.  
    14.     '"guess" the colour based on these values
    15. Dim sColourName As String
    16.   Select Case iRed
    17.   Case Is > 170     'lots of red
    18.       Select Case iGreen
    19.       Case Is > 170     'lots of green
    20.           Select Case iBlue  'blue:  lots/medium/little
    21.           Case Is > 170:  sColourName = "White"
    22.           Case Is > 85:   sColourName = "Bright Yellow"
    23.           Case Else:      sColourName = "Yellow"
    24.           End Select
    25.       Case Is > 85      'medium green
    26.           Select Case iBlue  'blue:  lots/medium/little
    27.           Case Is > 170:  sColourName = "Pink"
    28.           Case Is > 85:   sColourName = "Magenta"
    29.           Case Else:      sColourName = "Orange"
    30.           End Select
    31.       Case Else         'little green
    32.           Select Case iBlue  'blue:  lots/medium/little
    33.           Case Is > 170:  sColourName = "Purple"
    34.           Case Is > 85:   sColourName = "Dark Pink"
    35.           Case Else:      sColourName = "Red"
    36.           End Select
    37.       End Select
    38.  
    39.   Case Is > 85      'medium red
    40.       Select Case iGreen
    41.       Case Is > 170     'lots of green
    42.           Select Case iBlue  'blue:  lots/medium/little
    43.           Case Is > 170:  sColourName = "Cyan"
    44.           Case Is > 85:   sColourName = "Green"
    45.           Case Else:      sColourName = "Bright Green"
    46.           End Select
    47.       Case Is > 85      'medium green
    48.           Select Case iBlue  'blue:  lots/medium/little
    49.           Case Is > 170:  sColourName = "Dark Blue"
    50.           Case Is > 85:   sColourName = "Dark Grey"
    51.           Case Else:      sColourName = "Dark Green"
    52.           End Select
    53.       Case Else         'little green
    54.           Select Case iBlue  'blue:  lots/medium/little
    55.           Case Is > 170:  sColourName = "Dark Blue"
    56.           Case Is > 85:   sColourName = "Purple"
    57.           Case Else:      sColourName = "Dark Red"
    58.           End Select
    59.       End Select
    60.  
    61.   Case Else         'little red
    62.       Select Case iGreen
    63.       Case Is > 170     'lots of green
    64.           Select Case iBlue  'blue:  lots/medium/little
    65.           Case Is > 170:  sColourName = "Cyan"
    66.           Case Is > 85:   sColourName = "Green"
    67.           Case Else:      sColourName = "Bright Green"
    68.           End Select
    69.       Case Is > 85      'medium green
    70.           Select Case iBlue  'blue:  lots/medium/little
    71.           Case Is > 170:  sColourName = "Blue"
    72.           Case Is > 85:   sColourName = "Dark Cyan"
    73.           Case Else:      sColourName = "Dark Green"
    74.           End Select
    75.       Case Else         'little green
    76.           Select Case iBlue  'blue:  lots/medium/little
    77.           Case Is > 170:  sColourName = "Bright Blue"
    78.           Case Is > 85:   sColourName = "Dark Blue"
    79.           Case Else:      sColourName = "Black"
    80.           End Select
    81.       End Select
    82.    End Select
    83.  
    84.   ColourLongToWords = sColourName
    85.  
    86. End Function
    Note that I may not have chosen colour names that you like, but it is easy enough to change them!
    Last edited by si_the_geek; Oct 20th, 2005 at 10:36 AM.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: reading the color of pixels

    also how would i get the pixels by pixels for a picture?

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: reading the color of pixels

    Do you mean read each pixel in a picturebox?

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: reading the color of pixels

    Quote Originally Posted by si_the_geek
    Do you mean read each pixel in a picturebox?
    well wat im trying to do is make a program to do this

    load a picture box
    read each pixel from it 1 by 1 by 1 and if the pixel is white it will add a " " to the textbox and if its black it adds a ".". but i cant get the checking the pixels to work

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: reading the color of pixels

    you could have mentioned that before I wrote the Function above!

    All you need to do is create two nested loops (one for vertical and one for horizontal), and read each pixel using Point. You can then use a much shorter version of my function (or just use it as-is) to check whether the value is one of the colours you want.
    VB Code:
    1. Dim lX as Long, lY as Long
    2. Dim lPixelColour as Long
    3.   With Picture1
    4.      For lX = 1 to .Picture.Width
    5.        For lY = 1 to .Picture.Height
    6.          lPixelColour = .Point(lX,lY)
    7.          Select Case ColourLongToWords(lPixelColour)
    8.          Case "Black"
    9.            'do something
    10.          Case "White
    11.            'do something else
    12.          End Select
    13.        Next lY
    14.      Next lX
    15.   End With
    Note that this is untested, I'm not sure if the loops (or the parameters to Point) are correct.

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: reading the color of pixels

    Quote Originally Posted by si_the_geek
    you could have mentioned that before I wrote the Function above!

    All you need to do is create two nested loops (one for vertical and one for horizontal), and read each pixel using Point. You can then use a much shorter version of my function (or just use it as-is) to check whether the value is one of the colours you want.
    VB Code:
    1. Dim lX as Long, lY as Long
    2. Dim lPixelColour as Long
    3.   With Picture1
    4.      For lX = 1 to .Picture.Width
    5.        For lY = 1 to .Picture.Height
    6.          lPixelColour = .Point(lX,lY)
    7.          Select Case ColourLongToWords(lPixelColour)
    8.          Case "Black"
    9.            'do something
    10.          Case "White
    11.            'do something else
    12.          End Select
    13.        Next lY
    14.      Next lX
    15.   End With
    Note that this is untested, I'm not sure if the loops (or the parameters to Point) are correct.
    wat would i use in ur function to find if its black or white?

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: reading the color of pixels

    Those parts are already there. If you don't understand which parts to remove, it's probably best to leave it as-is.

    Note that there were a couple of typo's in the function, which I have now corrected.

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: reading the color of pixels

    Quote Originally Posted by si_the_geek
    Those parts are already there. If you don't understand which parts to remove, it's probably best to leave it as-is.

    Note that there were a couple of typo's in the function, which I have now corrected.
    it didnt work i got an error in the function it was overflow
    iGreen = (lColour And &HFFFF) \ &H100

  15. #15
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: reading the color of pixels

    Quote Originally Posted by high6
    ....how would i read just the first pixel of a picture i can do the rest.
    Quote Originally Posted by high6
    ....also how would i get the pixels by pixels for a picture?.
    C'mon some effort on your part is required... (Sorry Si)

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: reading the color of pixels

    Minor change, from this:
    iGreen = (lColour And &HFFFF) \ &H100

    To this:
    iGreen = (lColour And &HFF00) Mod &HFF


    I have only one thing to say Bruce, and that is:

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: reading the color of pixels

    well its late so im going to bed but i tryed and wat happened was it just wrote "." like 20 times across in 4 rows and that isnt wat the picture was can anoyone help?

  18. #18
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: reading the color of pixels

    What code did you try?

  19. #19
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: reading the color of pixels

    The code for green is still wrong. Should be
    VB Code:
    1. iGreen = (lColour \ &H100) And &HFF

  20. #20
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: reading the color of pixels

    it just wrote "." like 20 times across in 4 rows and that isnt wat the picture was
    This is probably because you need to adjust the thresholds in the function to catch the color you are trying to read. Can you upload a sample picture so we could try it?

  21. #21
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: reading the color of pixels

    Quote Originally Posted by moeur
    The code for green is still wrong. Should be
    VB Code:
    1. iGreen = (lColour \ &H100) And &HFF
    Indeed - I just checked again and my previous correction worked for several values of Blue, but not all. Now fixed above!

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