[GDI +] how to convert a color image to a black/white image

Source: Internet
Author: User

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...]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.