Results 1 to 11 of 11

Thread: vb.net get pixel color

  1. #1

    Thread Starter
    Banned
    Join Date
    Mar 2009
    Posts
    764

    vb.net get pixel color

    deva path

    Code:
    get pixel color ( in an image ) :
    Public Class Form1 ' I (moti barski) made this code (just saying)
        Dim curPixelX As Integer = 0
        Dim curPixelY As Integer = 0
        Dim r1, g1, b1 As String
        Dim curColorChar As Char = Nothing
        Dim bm As Bitmap
        Sub RGB_breakerBuster(ByVal color As String, ByRef red As String, ByRef green As String, ByRef blue As String)
            red = color.Substring(color.IndexOf("R=") + 2)
            color = red
            red = red.Substring(0, red.IndexOf(","))
            green = color.Substring(color.IndexOf("G=") + 2)
            color = green
            green = green.Substring(0, green.IndexOf(","))
            blue = color.Substring(color.IndexOf("B=") + 2)
            blue = blue.Substring(0, blue.IndexOf("]"))
        End Sub
        Public Function getPixelColor(ByVal red As String, ByVal green As String, ByVal blue As String) As Char
            Dim r, g, b As Integer
            Dim colorchar As Char
            r = red
            g = green
            b = blue
            If r > 245 And g > 245 And b > 245 Then
                colorchar = "w" ' white
            ElseIf r < 20 And g < 20 And b < 20 Then
                colorchar = "k" ' black (kuro in japanese)
            ElseIf r > g And g > b And g < 100 Then
                colorchar = "r" ' red
            ElseIf r > g And g > b And g > 200 Then
                colorchar = "y" ' yellow
            ElseIf r > g And g > b And 100 < g < 200 Then
                colorchar = "o" 'orange
            ElseIf (g > r And r > b) Or (g > b And b > r) Then
                colorchar = "g" 'green
            ElseIf b > g And g > r Then
                colorchar = "b" 'blue
            ElseIf (b > r And r > g) Or (r > b And g < 20) Then
                colorchar = "v" ' violet
            End If
            Return colorchar
        End Function
        Sub colorLegend()
    
            Select Case Label2.Text
                Case "w"
                    Label3.Text = "white"
    
                Case "k"
    
                    Label3.Text = "black"
                Case "r"
                    Label3.Text = "red"
                Case "y"
                    Label3.Text = "yellow"
                Case "o"
                    Label3.Text = "orange"
                Case "g"
                    Label3.Text = "green"
                Case "b"
                    Label3.Text = "blue"
                Case "v"
                    Label3.Text = "violate"
                Case Else
    
            End Select
    
        End Sub
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Try
                TextBox1.Text = bm.GetPixel(curPixelX, curPixelY).ToString()
                RGB_breakerBuster(TextBox1.Text, r1, g1, b1)
                TextBox2.Text = r1 & " " & g1 & " " & b1
                bm.SetPixel(curPixelX, curPixelY, Color.Black)
                PictureBox1.Image = bm
                curPixelX += 1
                Label2.Text = getPixelColor(r1, g1, b1)
                colorLegend()
            Catch ex As Exception
                Timer1.Enabled = False
                MsgBox("done")
            End Try
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Timer1.Enabled = Not Timer1.Enabled
            bm = PictureBox1.Image
            curPixelY = bm.Height \ 2
        End Sub
    End Class
    put a picture of the visible spectrum in the picturebox



    see post #9 for faster code
    Last edited by moti barski; May 13th, 2011 at 01:18 PM.

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: vb.net get pixel color

    Oh my gosh. Did you really use the string representation of a color from ToString() to extract the RGB values instead of the R, G, and B properties???
    Code:
    red = color.Substring(color.IndexOf("R=") + 2)
            color = red
            red = red.Substring(0, red.IndexOf(","))
            green = color.Substring(color.IndexOf("G=") + 2)
            color = green
            green = green.Substring(0, green.IndexOf(","))
            blue = color.Substring(color.IndexOf("B=") + 2)
            blue = blue.Substring(0, blue.IndexOf("]"))
    Code:
    TextBox1.Text = bm.GetPixel(curPixelX, curPixelY).ToString()
                RGB_breakerBuster(TextBox1.Text, r1, g1, b1)
    Please fix that...

  3. #3

    Thread Starter
    Banned
    Join Date
    Mar 2009
    Posts
    764

    Re: vb.net get pixel color

    .paul and shaggy hiker suggested some nice stuff
    http://www.vbforums.com/showthread.p...95#post4004995
    Last edited by moti barski; May 8th, 2011 at 10:54 PM.

  4. #4
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: vb.net get pixel color

    Yes, and you didn't implement it. At all.

  5. #5

    Thread Starter
    Banned
    Join Date
    Mar 2009
    Posts
    764

    Re: vb.net get pixel color

    you have 2 options :

    1 wait for me to implement (remember I am lazy)
    2 DIY
    Last edited by moti barski; May 9th, 2011 at 01:33 PM.

  6. #6
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: vb.net get pixel color

    I would assume that you would implement it, since it involves only copying and pasting 5 lines of code into your own, but apparently not.

    (I would never use this code in the first place, so those aren't my options, by the way.)

  7. #7

    Thread Starter
    Banned
    Join Date
    Mar 2009
    Posts
    764

    Re: vb.net get pixel color

    Quote Originally Posted by minitech View Post
    I would assume that you would implement it, since it involves only copying and pasting 5 lines of code into your own, but apparently not.

    (I would never use this code in the first place, so those aren't my options, by the way.)
    is that your way of asking me to add pink ?

  8. #8
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: vb.net get pixel color

    No.&#160;

  9. #9

    Thread Starter
    Banned
    Join Date
    Mar 2009
    Posts
    764

    Re: vb.net get pixel color

    Code:
    Public Class Form1 ' code amped by .paul
        Dim curPixelX As Integer = 0
        Dim curPixelY As Integer = 0
        Dim r1, g1, b1 As Integer
        Dim curColorChar As Char = Nothing
        Dim bm As Bitmap
        Sub RGB_breakerBuster(ByVal inColor As Color, ByRef red As Integer, ByRef green As Integer, ByRef blue As Integer)
            ' returns value of red,green,blue in a pixel of a bitmap as integers
            red = inColor.R
            green = inColor.G
            blue = inColor.B
        End Sub
        Public Function getPixelColor(ByVal r As Integer, ByVal g As Integer, ByVal b As Integer) As Char
            ' r= red, g = green, b = blue
            Dim colorchar As Char
            If r > 245 And g > 245 And b > 245 Then
                colorchar = "w" ' white
            ElseIf r < 20 And g < 20 And b < 20 Then
                colorchar = "k" ' black (kuro in japanese)
            ElseIf r > g And g > b And g < 100 Then
                colorchar = "r" ' red
            ElseIf r > g And g > b And g > 200 Then
                colorchar = "y" ' yellow
            ElseIf r > g And g > b And 100 < g < 200 Then
                colorchar = "o" 'orange
            ElseIf (g > r And r > b) Or (g > b And b > r) Then
                colorchar = "g" 'green
            ElseIf b > g And g > r Then
                colorchar = "b" 'blue
            ElseIf (b > r And r > g) Or (r > b And g < 20) Then
                colorchar = "v" ' violet
            Else
                colorchar = "u" ' yet undefined
            End If
            Return colorchar
        End Function
        Sub colorLegend()
            ' converts color char to the color name
            ' label2 = getPixelColor(r1, g1, b1) 1st colorchar
            ' label3 = color represented by colorchar
            Select Case Label2.Text
                Case "w"
                    Label3.Text = "white"
    
                Case "k"
    
                    Label3.Text = "black"
                Case "r"
                    Label3.Text = "red"
                Case "y"
                    Label3.Text = "yellow"
                Case "o"
                    Label3.Text = "orange"
                Case "g"
                    Label3.Text = "green"
                Case "b"
                    Label3.Text = "blue"
                Case "v"
                    Label3.Text = "violate"
                Case Else
    
            End Select
    
        End Sub
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Try
                TextBox1.Text = bm.GetPixel(curPixelX, curPixelY).ToString()
                RGB_breakerBuster(bm.GetPixel(curPixelX, curPixelY), r1, g1, b1)
                TextBox2.Text = r1 & " " & g1 & " " & b1
                bm.SetPixel(curPixelX, curPixelY, Color.Black)
                PictureBox1.Image = bm
                curPixelX += 1
                Label2.Text = getPixelColor(r1, g1, b1)
                colorLegend()
            Catch ex As Exception
                Timer1.Enabled = False
                MsgBox("done")
            End Try
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Timer1.Enabled = Not Timer1.Enabled
            bm = PictureBox1.Image
            curPixelY = bm.Height \ 2
        End Sub
    End Class
    Last edited by moti barski; May 13th, 2011 at 02:32 PM.

  10. #10
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: vb.net get pixel color

    Violate? I think that violates the rules defining the standard colors A quick question - what is the significance of the name RGB_breakerBuster? Why do you need a Sub to even do that? Another one - what is the Timer for??

  11. #11

    Thread Starter
    Banned
    Join Date
    Mar 2009
    Posts
    764

    Re: vb.net get pixel color

    violet is in the visible spectrum, was it mispelled, meh

    RGB_breakerBuster breakes down the pixel color string to the red, green blue color value (as integer)
    just like in mortal kombat the breakerBuster breakes the opponents combo

    since when do I answer why questions ?

    what is the Timer for??
    run the program

Tags for this Thread

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