C # Image Processing (II)-black and white Effects

Source: Internet
Author: User

Today, let's talk about how to make a picture black and white, that is, to make the image black and white. The method for opening the image has been published. The specific address is:

C # Image Processing (I)

 

In the face of another image that needs to be processed as a black/white image, I wrote the following function for it,

/// <Summary>
/// Convert the image to a black or white image
/// </Summary>
/// <Param name = "mybt"> image to be processed </param>
/// <Param name = "width"> image length </param>
/// <Param name = "height"> image height </param>
/// <Returns> processed black and white images </returns>
Public Bitmap BWPic (Bitmap mybm, int width, int height)
{
Bitmap bm = new Bitmap (width, height); // initialize a Bitmap object to record the processed image
Int x, y, result; // x, y indicates the number of cycles, and result indicates the processed pixel value.
Color pixel;

For (x = 0; x <width; x ++)
{
For (y = 0; y {
Pixel = mybm. GetPixel (x, y); // obtain the pixel value of the current coordinate.
Result = (pixel. R + pixel. G + pixel. B)/3; // average value of three colors: Red, green, and blue

// Plot and assign the processed value to the defined bm object
Bm. SetPixel (x, y, Color. FromArgb (result, result, result ));
}
}

Return bm; // return the black-and-white image
}

Finally, a pictureBox control is used to present the processed image. The following statement is used,

Int width = this. pictureBox. Width; // The length of the image container.
Int height = this. pictureBox. Height; // The width of the image container.
This. pictureBox. Image = pix. BWPic (Bitmap) this. pictureBox. Image, width, height); // process the Image

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.