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;
}

Pixelate an image with c#

Pixelating an image in c# is on par with blurring an image – not terribly complex. This method accepts an image, a pixelate region (rectangle), a pixelate size and returns a bitmap.

private static Bitmap Pixelate(Bitmap image, Rectangle rectangle, Int32 pixelateSize)
{
    Bitmap pixelated = new System.Drawing.Bitmap(image.Width, image.Height);

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

    // look at every pixel in the rectangle while making sure we're within the image bounds
    for (Int32 xx = rectangle.X; xx < rectangle.X + rectangle.Width && xx < image.Width; xx += pixelateSize)
    {
        for (Int32 yy = rectangle.Y; yy < rectangle.Y + rectangle.Height && yy < image.Height; yy += pixelateSize)
        {
            Int32 offsetX = pixelateSize / 2;
            Int32 offsetY = pixelateSize / 2;

            // make sure that the offset is within the boundry of the image
            while (xx + offsetX >= image.Width) offsetX--;
            while (yy + offsetY >= image.Height) offsetY--;

            // get the pixel color in the center of the soon to be pixelated area
            Color pixel = pixelated.GetPixel(xx + offsetX, yy + offsetY);

            // for each pixel in the pixelate size, set it to the center color
            for (Int32 x = xx; x < xx + pixelateSize && x < image.Width; x++)
                for (Int32 y = yy; y < yy + pixelateSize && y < image.Height; y++)
                    pixelated.SetPixel(x, y, pixel);
        }
    }

    return pixelated;
}

The pixelate method looks at ever block of pixels in the pixelate size, grabs the middle pixel and then sets all of the pixels in the block to that same color. If you don’t use the middle pixel, the image ends up shifting (my initial version looked at the top-left pixel.)

Dad at Christmas

Dad at Christmas

Pixelated Dad at Christmas

Pixelated Dad at Christmas

If you want to pixelate the whole image, just use the bounds of the original image as the pixelate rectangle. Here’s an example of how to overload the Pixelate() method:

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

Download original dad image.

Blur an image with c#

Blurring an image with c# is pretty simple. I was messing around with something one night and needed to blur certain regions of JPGs that I was processing. This method accepts an image, a blur region (rectangle), a blur size and returns a blurred bitmap.

private static Bitmap Blur(Bitmap image, Rectangle rectangle, Int32 blurSize)
{
    Bitmap blurred = new Bitmap(image.Width, image.Height);

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

    // look at every pixel in the blur rectangle
    for (Int32 xx = rectangle.X; xx < rectangle.X + rectangle.Width; xx++)
    {
        for (Int32 yy = rectangle.Y; yy < rectangle.Y + rectangle.Height; yy++)
        {
            Int32 avgR = 0, avgG = 0, avgB = 0;
            Int32 blurPixelCount = 0;

            // average the color of the red, green and blue for each pixel in the
            // blur size while making sure you don't go outside the image bounds
            for (Int32 x = xx; (x < xx + blurSize && x < image.Width); x++)
            {
                for (Int32 y = yy; (y < yy + blurSize && y < image.Height); y++)
                {
                    Color pixel = blurred.GetPixel(x, y);

                    avgR += pixel.R;
                    avgG += pixel.G;
                    avgB += pixel.B;

                    blurPixelCount++;
                }
            }

            avgR = avgR / blurPixelCount;
            avgG = avgG / blurPixelCount;
            avgB = avgB / blurPixelCount;

            // now that we know the average for the blur size, set each pixel to that color
            for (Int32 x = xx; x < xx + blurSize && x < image.Width && x < rectangle.Width; x++)
                for (Int32 y = yy; y < yy + blurSize && y < image.Height && y < rectangle.Height; y++)
                    blurred.SetPixel(x, y, Color.FromArgb(avgR, avgG, avgB));
        }
    }

    return blurred;
}

The method takes a look at every pixel within the blur rectangle and samples the reds, greens and blues within the blur size to figure out an average. Then it sets every pixel within the blur size square to the average color.

Jellyfish at the Monterey Aquarium

Jellyfish at the Monterey Aquarium

Blurred jellyfish at the Monterey Aquarium

Blurred jellyfish at the Monterey Aquarium

If you want to blur the whole image, just use the bounds of the original image as the blur rectangle. Here’s an example of how to overload the Blur() method:

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

Download original jellyfish image.