Dim Threshold as Int
Dim TotalSum as Long
Dim b as Bitmap
For i = 0 to b.Width-1
For j = 0 to b.Height-1
If b.GetPixel(i,j) > Threshold then
b.SetPixel(i,j) = Colors.White
Else
b.SetPixel(i,j) = Colors.Black
End If
TotalSum = TotalSum + b.GetPixel(i,j)
Next
Next
Dim AvgThreshold as Int
AvgThreshold = TotalSum/ (b.Width * b.Height)