01-20-2012، 01:16 PM
Convert an image to pure Black-and-White
کد پیاچپی:
Instructions: To use this function, first pass the desired image to it,
and then pass the Mode parameter, which determines if the image
should be processes based on the total RGB value of a pixel or the lightness of a pixel.
Then pass the Tolerance value, which is a decimal which must be >= -1 and
سورس کد :
کد پیاچپی:
Public Function PureBW(ByVal image As System.Drawing.Bitmap, Optional ByVal Mode As BWMode = BWMode.By_Lightness, Optional ByVal tolerance As Single = 0) As System.Drawing.Bitmap
Dim x As Integer
Dim y As Integer
If tolerance > 1 Or tolerance < -1 Then
Throw New ArgumentOutOfRangeException
Exit Function
End If
For x = 0 To image.Width - 1 Step 1
For y = 0 To image.Height - 1 Step 1
Dim clr As Color = image.GetPixel(x, y)
If Mode = BWMode.By_RGB_Value Then
If (CInt(clr.R) + CInt(clr.G) + CInt(clr.B)) > 383 - (tolerance * 383) Then
image.SetPixel(x, y, Color.White)
Else
image.SetPixel(x, y, Color.Black)
End If
Else
If clr.GetBrightness > 0.5 - (tolerance / 2) Then
image.SetPixel(x, y, Color.White)
Else
image.SetPixel(x, y, Color.Black)
End If
End If
Next
Next
Return image
End Function
Enum BWMode
By_Lightness
By_RGB_Value
End Enum