Make an image black and white in c#

I’ve gone on a strange image manipulation kick in c# recently. Partly because I needed blurring method for something (then pixelation spawned from that) and then partially because it was fun. Here’s how to remove color from an image in c#:

private static Bitmap BlackAndWhite(Bitmap image, Rectangle rectangle)
{
    Bitmap blackAndWhite = new System.Drawing.Bitmap(image.Width, image.Height);

    // make an exact copy of the bitmap provided
    using(Graphics graphics = System.Drawing.Graphics.FromImage(blackAndWhite))
        graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
            new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);

    // for every pixel in the rectangle region
    for (Int32 xx = rectangle.X; xx < rectangle.X + rectangle.Width && xx < image.Width; xx++)
    {
        for (Int32 yy = rectangle.Y; yy < rectangle.Y + rectangle.Height && yy < image.Height; yy++)
        {
            // average the red, green and blue of the pixel to get a gray value
            Color pixel = blackAndWhite.GetPixel(xx, yy);
            Int32 avg = (pixel.R + pixel.G + pixel.B) / 3;

            blackAndWhite.SetPixel(xx, yy, Color.FromArgb(0, avg, avg, avg));
        }
    }

    return blackAndWhite;
}

The BlackAndWhite() method takes a look at every pixel in the region and averages the red, green and blue values to get a gray value. Once a gray value is determined, the pixel is set to that color.

Joe's Crab Shack in San Francisco

Joe's Crab Shack in San Francisco

Black and White Joe's Crab Shack in San Francisco

Black and White Joe's Crab Shack in San Francisco

If you want to make the entire image black and white and not just a region (rectangle), then overload the BlackAndWhite() method like this:

private static Bitmap BlackAndWhite(Bitmap image)
{
    return BlackAndWhite(image, new Rectangle(0, 0, image.Width, image.Height));
}

Download the original Joe’s Crab Shack image.

*Update: I was poking around on the Internet, looking at things and stuff and found a better greyscale implementation (read: faster, much faster and a bit more complex) from Switch On The Code. Check it out:

public static Bitmap MakeGrayscale3(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
   ColorMatrix colorMatrix = new 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
   ImageAttributes attributes = new 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;
}


Comments are closed.