Post
by robertrndya » Tue May 31, 2011 3:33 pm
Hi Brad,
I've been breaking my head with vb of late. But I still haven't managed to prepare the software. I managed to get a code on the net for scanning an image. I modified it a lil and got rid of the errors that were there. The code was supposed to find rgb values within a certain range and replace them with red color. I removed that replacing part out and tried to display the scanned values in a text box that i put in the form. My main aim is to get values in form of 3-bit binary, like the excel sheet does. So I can then segregate them into rgb.
I tried different methods but I just cant get it right. I tried converting the rgb values to hex and displaying in the textbox, to my surprise the hex values were css hex values (hex triplets). And I could picture the image of google in the values. But that's NOT what i want, how am I supposed to get the 3-bit binary values????? I went to the extent of Comparing the scanned values with a range of values which I got from the net for different colors, but it still didn't work. I tried to scan for the 7 colors but it could only recognize black and white color (although I don't understand where there is black color in the google logo)
This is the code:
Public Class Form1
Dim image1 As Bitmap
Dim ofd As New OpenFileDialog
Dim pb1 As New PictureBox
Private Sub pb1_click(ByVal ByValsenderAsPictureBox, ByVal ByValeAsEventArgs)
ofd.ShowDialog()
image1 = New Bitmap(ofd.FileName, True)
Dim x, y, q As Integer
Dim pixelColor As Color 'Loopthroughtheimagespixels.
' Dim newColor As Color
' Dim array(image1.Height, image1.Width) As Char
Dim r As Long, g As Long, b As Long
Dim l As Char, m As Char, n As Char
Dim p As Long
For x = 0 To image1.Width - 1
For y = 0 To image1.Height - 1
pixelColor = image1.GetPixel(x, y)
With pixelColor
'If .R < 200 And .R > 100 And .G < 200 And .G > 100 And .B < 200 And .B > 100 Then
r = Val(.R) ' newColor = Color.FromArgb(255, 0, 0)
g = Val(.G) ' image1.SetPixel(x, y, newColor)
b = Val(.B)
'If r < 100 And r > 9 Then
' r = 0 & r
'ElseIf r < 10 Then
' r = 0 & r
'End If
'If g < 100 And g > 9 Then
' g = 0 & g
'ElseIf g < 10 Then
' g = 0 & g
'End If
'If b < 100 And b > 9 Then
' b = 0 & b
'ElseIf b < 10 Then
' b = 0 & b
'End If
'If r = 255 Or r = 238 Or r = 221 Or r = 204 Or r = 187 Or r = 170 Or r = 153 Or r = 136 Or r = 119 Or r = 102 Or r = 85 Or r = 68 Then
' l = 1
'Else
' l = 0
'End If
'If g = 255 Or g = 238 Or g = 221 Or g = 204 Or g = 187 Or g = 170 Or g = 153 Or g = 136 Or g = 119 Or g = 102 Or g = 85 Or g = 68 Then
' m = 0
'Else
' m = 1
'End If
'If b = 255 Or b = 238 Or b = 221 Or b = 204 Or b = 187 Or b = 170 Or b = 153 Or b = 136 Or b = 119 Or b = 102 Or b = 85 Or b = 68 Then
' n = 0
'Else
' n = 1
'End If
'array(x, y) = l & m & n
l = Hex(r)
m = Hex(g)
n = Hex(b)
p = r & g & b
If (p = 0 Or p = 333 Or p = 555 Or p = 888 Or p = 101010 Or p = 131313 Or p = 151515 Or p = 181818 Or p = 202020 Or p = 232323 Or p = 262626 Or p = 282828 Or p = 313131 Or p = 333333 Or p = 343434 Or p = 363636 Or p = 383838 Or p = 414141 Or p = 434343 Or p = 464646 Or p = 484848 Or p = 515151 Or p = 545454 Or p = 565656 Or p = 595959 Or p = 616161 Or p = 646464 Or p = 666666 Or p = 696969 Or p = 717171 Or p = 747474 Or p = 777777 Or p = 797979 Or p = 828282 Or p = 848484 Or p = 858585 Or p = 878787 Or p = 888888 Or p = 898989 Or p = 929292 Or p = 949494 Or p = 979797 Or p = 999999 Or p = 102102102 Or p = 105105105 Or p = 107107107 Or p = 110110110 Or p = 112112112 Or p = 115115115 Or p = 117117117 Or p = 119119119 Or p = 120120120 Or p = 122122122 Or p = 125125125 Or p = 127127127 Or p = 130130130 Or p = 133133133 Or p = 135135135 Or p = 136136136 Or p = 138138138 Or p = 140140140 Or p = 143143143 Or p = 145145145 Or p = 148148148 Or p = 150150150 Or p = 153153153 Or p = 156156156 Or p = 158158158 Or p = 161161161 Or p = 163163163 Or p = 166166166 Or p = 168168168 Or p = 171171171 Or p = 173173173 Or p = 176176176 Or p = 179179179 Or p = 181181181 Or p = 184184184 Or p = 186186186 Or p = 187187187 Or p = 189189189) Then
q = 0 'black
ElseIf (p = 13843226 Or p = 95159159 Or p = 95158160 Or p = 152245255 Or p = 142229238 Or p = 122197205 Or p = 83134139 Or p = 6666111 Or p = 100149237 Or p = 7261139 Or p = 206209 Or p = 191255 Or p = 178238 Or p = 154205 Or p = 104139 Or p = 30144255 Or p = 28134238 Or p = 24116205 Or p = 1678139 Or p = 135206250 Or p = 132112255 Or p = 205 Or p = 123104238 Or p = 72209204 Or p = 2525112 Or p = 128 Or p = 65105225 Or p = 72118255 Or p = 67110238 Or p = 5895205 Or p = 3964139 Or p = 34102 Or p = 135206235 Or p = 126192238 Or p = 108166205 Or p = 10690205 Or p = 131111255 Or p = 122103238 Or p = 10589205 Or p = 7160139 Or p = 70130180 Or p = 99184255 Or p = 92172238 Or p = 79148205 Or p = 54100139 Or p = 51102153 Or p = 51153204 Or p = 102153204 Or p = 127255212 Or p = 118238198 Or p = 102205170 Or p = 69139116 Or p = 255 Or p = 238 Or p = 205 Or p = 139 Or p = 128 Or p = 3624130 Or p = 3535142 Or p = 7777255 Or p = 156 Or p = 8989171 Or p = 50153204 Or p = 127255 Or p = 56176222 Or p = 3180200 Or p = 6586197) Then
q = 1 'blue
ElseIf (p = 15320450 Or p = 477947 Or p = 1000 Or p = 3413934 Or p = 17325547 Or p = 1242520 Or p = 5020550 Or p = 60179113 Or p = 250154 Or p = 10714235 Or p = 19225562 Or p = 17923858 Or p = 15420550 Or p = 10513934 Or p = 152251152 Or p = 154255154 Or p = 144238144 Or p = 124205124 Or p = 8413984 Or p = 4613987 Or p = 84255159 Or p = 78238148 Or p = 67205128 Or p = 255127 Or p = 238118 Or p = 205102 Or p = 13969 Or p = 15420550 Or p = 1272550 Or p = 1182380 Or p = 1022050 Or p = 691390 Or p = 2550 Or p = 1280 Or p = 2380 Or p = 2050 Or p = 1390 Or p = 3514235 Or p = 6611166 Or p = 1272550 Or p = 35142104 Or p = 255127 Or p = 924917 Or p = 2157116) Then
q = 2 'green
ElseIf (p = 255255 Or p = 238238 Or p = 205205205 Or p = 138138138 Or p = 64224208 Or p = 245255 Or p = 229238 Or p = 197205 Or p = 127255212 Or p = 118238198 Or p = 102205170 Or p = 72209204 Or p = 112219219) Then
q = 3 'cyan
ElseIf (p = 12800 Or p = 1783434 Or p = 2554848 Or p = 2384444 Or p = 2053838 Or p = 1392626 Or p = 2045151 Or p = 25500 Or p = 23800 Or p = 20500 Or p = 13900 Or p = 2559971 Or p = 2389266 Or p = 2057957 Or p = 1395438 Or p = 1423535 Or p = 1402323 Or p = 19200) Then
q = 4 'red
ElseIf (p = 255193193 Or p = 238180180 Or p = 240128128 Or p = 250128114 Or p = 25520147 Or p = 23818137 Or p = 20516118 Or p = 255105180 Or p = 255110180 Or p = 238106167 Or p = 20596144 Or p = 1395898 Or p = 1391080 Or p = 19921133 Or p = 2059292 Or p = 255106106 Or p = 2389999 Or p = 2058585 Or p = 255182193 Or p = 255174185 Or p = 238162173 Or p = 205140149 Or p = 13095101 Or p = 219112147 Or p = 255130171 Or p = 238121159 Or p = 205104137 Or p = 1397193 Or p = 20832144 Or p = 25562150 Or p = 23858140 Or p = 20550120 Or p = 1393482 Or p = 25528174 Or p = 22791216) Then
q = 5 'pink
ElseIf (p = 18413411 Or p = 25518515 Or p = 23817314 Or p = 20514912 Or p = 1391018 Or p = 21816532 Or p = 25519337 Or p = 23818034 Or p = 20515529 Or p = 2552550 Or p = 2382380 Or p = 2052050 Or p = 1391390 Or p = 2552150 Or p = 2382010 Or p = 2051730) Then
q = 6 'yellow
ElseIf (p = 209209209 Or p = 212212212 Or p = 214214214 Or p = 217217217 Or p = 219219219 Or p = 221221221 Or p = 222222222 Or p = 224224224 Or p = 227227227 Or p = 229229229 Or p = 232232232 Or p = 235235235 Or p = 237237237 Or p = 238238238 Or p = 240240240 Or p = 242242242 Or p = 245245245 Or p = 247247247 Or p = 250250250 Or p = 252252252 Or p = 255255255) Then
q = 7 'white
Else : TextBox1.AppendText("ERROR ")
End If
'If .R = 0 And .G = 0 Then
TextBox1.AppendText(q & " ")
'End If
'TextBox1.AppendText(l & m & n & " ")
'End If
End With
Next
Next
pb1.Image = image1
End Sub
Private Sub Form1_Load(ByVal ByValsenderAsObject, ByVal ByValeAsEventArgs) Handles Me.Load
Controls.Add(pb1)
pb1.Dock = DockStyle.Fill
AddHandler pb1.Click, AddressOf pb1_click
End Sub
End Class
-------------------------
And when I try to store the values in the array I get an error saying the value exceeds the bounds of the array
Also please explain me the following statements:
Private Sub pb1_click(ByVal ByValsenderAsPictureBox, ByVal ByValeAsEventArgs)
ofd.ShowDialog()
image1 = New Bitmap(ofd.FileName, True)
pb1.Image = image1
Private Sub Form1_Load(ByVal ByValsenderAsObject, ByVal ByValeAsEventArgs) Handles Me.Load
Controls.Add(pb1)
pb1.Dock = DockStyle.Fill
AddHandler pb1.Click, AddressOf pb1_click
End Sub