When converting a color image to a black-and-white image, you need to calculate the valid brightness value of each pixel in the image.
Brightness values can be easily converted to black and white images.
The following formula can be used to calculate the valid brightness of a pixel:
Y = 0.3red + 0.59 green + 0.11 blue
Use color. fromargb (Y, Y, Y) to convert the calculated value.
ConversionCodeYou can use the following method:
[C #]
Public Bitmap converttograyscale (bitmap source)
{
Bitmap BM = New Bitmap (source. Width, source. Height );
For ( Int Y = 0 ; Y < BM. height; y ++ )
{
For ( Int X = 0 ; X < BM. width; x ++ )
{
Color C = Source. getpixel (x, y );
Int Luma = ( Int ) (C. R * 0.3 + C. g * 0.59 + C. B * 0.11 );
BM. setpixel (X, Y, color. fromargb (Luma, Luma, Luma ));
}
}
Return BM;
}
[VB]
Public Function converttograyscale () Function Converttograyscale ( Byval Source As Bitmap) As Bitmap
Dim BM As New Bitmap (source. Width, source. Height)
Dim X
Dim Y
For Y = 0 To BM. Height
For X = 0 To BM. Width
Dim C As Color = Source. getpixel (x, y)
Dim Luma As Integer = CINT (C. R * 0.3 + C. g * 0.59 + C. B * 0.11 )
BM. setpixel (X, Y, color. fromargb (Luma, Luma, Luma)
Next
Next
Return BM
End Function
Of course, this is a good method. You can also use the colormatrix class if you need to make color conversion easier. Next we will introduce
[To be continued...]