I will provide you with working code so you can see what you are doing wrong. Nishant Sivakumar pointed to the location where the error is, so look at this example and try to correct your code.
Public g_RowSizeBytes As Integer
Public rgbValues() As Byte
Private bmpdata As Imaging.BitmapData
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
02
03 Dim bmp As New System.Drawing.Bitmap(picViewer1.Image)'IMAGE FROM PICTURE BOX...
04 Call LockBitmap(bmp) 'LOCKING BITMAP
06
07 Dim RedValue As Int32
08 Dim GreenValue As Int32
09 Dim BlueValue As Int32
10 Dim l As Integer = 0 ' Pixel position
11 For x = 0 To bmp.Width - 1 'Pixels start with 0 so we need Width - 1
12 For y = 0 To bmp.Height - 1 'Pixels start with 0 so we need Height - 1
13
14 l = ((bmp.Width * 3 * y) + (x * 3)) ' pixel is made of 3 parts (RGB colors)just to understand ...
15 RedValue = rgbValues(l)
16 GreenValue = rgbValues(l + 1)
17 BlueValue = rgbValues(l + 2)
18 If RedValue > 250 AndAlso GreenValue > 250 AndAlso BlueValue > 250 Then
19 Msgbox("Pixel is of a white color...")20 ElseIf RedValue < 5 AndAlso GreenValue < 5 AndAlso BlueValue < 5 Then
21 Msgbox("Pixel is of a black color..."
22 Else
23 Msgbox("Pixel is not white or black ...")
24 End If
25 Next
26 Next
27 Call UnlockBitmap(bmp) 'UNLOCKING BITMAP
29
30 picViewer1.Image = (bmp)
31 picViewer1.Refresh()
32
33 End Sub
34
35 Private Sub LockBitmap(ByVal bmp As Bitmap)
36 Dim bounds As Rectangle = New Rectangle(0, 0, bmp.Width, bmp.Height)
37 bmpdata = bmp.LockBits(bounds, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format24bppRgb)
38 g_RowSizeBytes = bmpdata.Stride
39 Dim total_size As Integer = bmpdata.Stride * bmpdata.Height
40 ReDim rgbValues(total_size)
41 Marshal.Copy(bmpdata.Scan0, rgbValues, 0, total_size)
42 End Sub
43
44 Private Sub UnlockBitmap(ByVal bmp As Bitmap)
45 Dim total_size As Integer = bmpdata.Stride * bmpdata.Height
46 Marshal.Copy(rgbValues, 0, bmpdata.Scan0, total_size)
47 bmp.UnlockBits(bmpdata)
48 rgbValues = Nothing
49 bmpdata = Nothing
50 End Sub