c# .net

Convert image to black and white in c#

Convert image to black and white in c#, someone asked me to explain?

In this article I will show you how to convert picture to black and white using c#. You need to pass the bitmap image to the following function. It has code,It will take the average of RGB values, in order to get the appropriate gray scale value and returns the result as bitmap object.

convert image to grayscale

C# image manipulation:

  public static Bitmap SetGrayscale(Bitmap btmap)
            Bitmap temp = (Bitmap)btmap;
            Bitmap bmap = (Bitmap)temp.Clone();
            Color c;
            for (int i = 0; i < bmap.Width; i++)
                for (int j = 0; j < bmap.Height; j++)
                    c = bmap.GetPixel(i, j);
                    byte gray = (byte)(.299 * c.R + .587 * c.G +.114 * c.B);
                    bmap.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
            return (Bitmap)bmap.Clone();       }


 Create your own grayscale image convertor using this above C# code.

Post your comments / questions