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.
Printable View
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.
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!
Posibly by using the GetPixel API:
Quote:
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.
Quote:
Originally Posted by si_the_geek
Kewl :)
how would i tell wat color it is?Quote:
Originally Posted by si_the_geek
I'll reverse engineer this.
http://www.vbforums.com/showpost.php...3&postcount=29
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.
Note that I may not have chosen colour names that you like, but it is easy enough to change them!VB Code:
Private Function ColourLongToWords(ByVal lColour As Long) As String 'Converts an RGB colour number to a string equivalent 'By Si_the_geek, VBForums 'ensure value is within range for colours lColour = lColour And &HFFFFFF 'convert to separate RGB values Dim iRed As Integer, iGreen As Integer, iBlue As Integer iRed = lColour And &HFF iGreen = (lColour \ &H100) And &HFF iBlue = lColour \ &H10000 '"guess" the colour based on these values Dim sColourName As String Select Case iRed Case Is > 170 'lots of red Select Case iGreen Case Is > 170 'lots of green Select Case iBlue 'blue: lots/medium/little Case Is > 170: sColourName = "White" Case Is > 85: sColourName = "Bright Yellow" Case Else: sColourName = "Yellow" End Select Case Is > 85 'medium green Select Case iBlue 'blue: lots/medium/little Case Is > 170: sColourName = "Pink" Case Is > 85: sColourName = "Magenta" Case Else: sColourName = "Orange" End Select Case Else 'little green Select Case iBlue 'blue: lots/medium/little Case Is > 170: sColourName = "Purple" Case Is > 85: sColourName = "Dark Pink" Case Else: sColourName = "Red" End Select End Select Case Is > 85 'medium red Select Case iGreen Case Is > 170 'lots of green Select Case iBlue 'blue: lots/medium/little Case Is > 170: sColourName = "Cyan" Case Is > 85: sColourName = "Green" Case Else: sColourName = "Bright Green" End Select Case Is > 85 'medium green Select Case iBlue 'blue: lots/medium/little Case Is > 170: sColourName = "Dark Blue" Case Is > 85: sColourName = "Dark Grey" Case Else: sColourName = "Dark Green" End Select Case Else 'little green Select Case iBlue 'blue: lots/medium/little Case Is > 170: sColourName = "Dark Blue" Case Is > 85: sColourName = "Purple" Case Else: sColourName = "Dark Red" End Select End Select Case Else 'little red Select Case iGreen Case Is > 170 'lots of green Select Case iBlue 'blue: lots/medium/little Case Is > 170: sColourName = "Cyan" Case Is > 85: sColourName = "Green" Case Else: sColourName = "Bright Green" End Select Case Is > 85 'medium green Select Case iBlue 'blue: lots/medium/little Case Is > 170: sColourName = "Blue" Case Is > 85: sColourName = "Dark Cyan" Case Else: sColourName = "Dark Green" End Select Case Else 'little green Select Case iBlue 'blue: lots/medium/little Case Is > 170: sColourName = "Bright Blue" Case Is > 85: sColourName = "Dark Blue" Case Else: sColourName = "Black" End Select End Select End Select ColourLongToWords = sColourName End Function
also how would i get the pixels by pixels for a picture?
Do you mean read each pixel in a picturebox?
well wat im trying to do is make a program to do thisQuote:
Originally Posted by si_the_geek
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
:lol: 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.
Note that this is untested, I'm not sure if the loops (or the parameters to Point) are correct.VB Code:
Dim lX as Long, lY as Long Dim lPixelColour as Long With Picture1 For lX = 1 to .Picture.Width For lY = 1 to .Picture.Height lPixelColour = .Point(lX,lY) Select Case ColourLongToWords(lPixelColour) Case "Black" 'do something Case "White 'do something else End Select Next lY Next lX End With
wat would i use in ur function to find if its black or white?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 overflowQuote:
Originally Posted by si_the_geek
iGreen = (lColour And &HFFFF) \ &H100
Quote:
Originally Posted by high6
C'mon some effort on your part is required... (Sorry Si)Quote:
Originally Posted by high6
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: :lol:
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?
What code did you try?
The code for green is still wrong. Should beVB Code:
iGreen = (lColour \ &H100) And &HFF
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?Quote:
it just wrote "." like 20 times across in 4 rows and that isnt wat the picture was
Indeed - I just checked again and my previous correction worked for several values of Blue, but not all. Now fixed above!Quote:
Originally Posted by moeur