c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
c# .net

Convert image to black and white in c#

| | ASP-NET , CSharp

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.

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.