Converts the input bitmap into a grayscale bitmap. The input bitmap is not modified. - CSharp System.Drawing

CSharp examples for System.Drawing:Image Convert

Description

Converts the input bitmap into a grayscale bitmap. The input bitmap is not modified.

Demo Code

// Copyright (c) .NET Foundation. All rights reserved.
using System.Drawing;
using System;/*from www. java 2 s.c o  m*/

public class Main{
        /// <summary>
        /// Converts the input bitmap into a grayscale bitmap.  The input bitmap is not modified.
        /// </summary>
        /// <param name="input">Input bitmap.</param>
        /// <returns>Grayscale version of the input bitmap.</returns>
        public static Bitmap ToGrayscale(Bitmap input)
        {
            //   Create the output bitmap.
            Bitmap output = new Bitmap(input.Width, input.Height);

            //   Convert the pixels.
            for (int x = 0; x < output.Width; x++)
                for (int y = 0; y < output.Height; y++)
                {
                    //   Get the input pixel color.
                    Color color = input.GetPixel(x, y);

                    //   Compute the grayscale value as the average of the RGB values.
                    byte grayscale = (byte)(((int)color.R + (int)color.G + (int)color.B)/3);

                    //   Set the output pixel.
                    output.SetPixel(x, y, Color.FromArgb(color.A, grayscale, grayscale, grayscale));
                }

            //   Done.  Return the output bitmap.
            return output;
        }
}

Related Tutorials