Analysis of the implementation code of C # color image grayscale algorithm

Source: Internet
Author: User

Http://www.jb51.net/article/37067.htm

public static Bitmap Makegrayscale (Bitmap original)
{
Create a blank bitmap the same size as original
Bitmap Newbitmap = new Bitmap (original. Width, original. Height);
Get a Graphics object from the new image
Graphics g = graphics.fromimage (Newbitmap);
Create the Grayscale ColorMatrix
System.Drawing.Imaging.ColorMatrix ColorMatrix = new System.Drawing.Imaging.ColorMatrix (
New float[][]
{
New float[] {. 3f,. 3f,. 3f, 0, 0},
New float[] {. 59f,. 59f,. 59f, 0, 0},
New float[] {. 11f,. 11f,. 11f, 0, 0},
New float[] {0, 0, 0, 1, 0},
New float[] {0, 0, 0, 0, 1}
});
Create some image attributes
System.Drawing.Imaging.ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes ();
Set the color matrix attribute
Attributes. SetColorMatrix (ColorMatrix);
Draw the original image on the new image
Using the grayscale color matrix
G.drawimage (Original, new Rectangle (0, 0, original. Width, original. Height),
0, 0, original. Width, original. Height, GraphicsUnit.Pixel, attributes);
Dispose the Graphics object
G.dispose ();
return newbitmap;
}

Analysis of the implementation code of C # color image grayscale algorithm

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.