Use an ImageAttributes object to convert an image to monochrome in C#

convert an image to monochrome

This example shows a quick and easy way to convert an image to monochrome. The example Quickly convert an image to grayscale in C# shows one way to convert an image to grayscale. It does that buy looping through the image’s pixels and setting each pixel’s red, green, and blue color components to a weighted average of those components. This example uses an ImageAttributes object to make a similar conversion even more quickly.

The following ToMonochrome method converts an image into monochrome.

// Convert an image to monochrome.
private Bitmap ToMonochrome(Image image)
{
    // Make the ColorMatrix.
    ColorMatrix cm = new ColorMatrix(new float[][]
    {
        new float[] {0.299f, 0.299f, 0.299f, 0, 0},
        new float[] {0.587f, 0.587f, 0.587f, 0, 0},
        new float[] {0.114f, 0.114f, 0.114f, 0, 0},
        new float[] { 0, 0, 0, 1, 0},
        new float[] { 0, 0, 0, 0, 1}
    });
    ImageAttributes attributes = new ImageAttributes();
    attributes.SetColorMatrix(cm);

    // Draw the image onto the new bitmap while
    // applying the new ColorMatrix.
    Point[] points =
    {
        new Point(0, 0),
        new Point(image.Width, 0),
        new Point(0, image.Height),
    };
    Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);

    // Make the result bitmap.
    Bitmap bm = new Bitmap(image.Width, image.Height);
    using (Graphics gr = Graphics.FromImage(bm))
    {
        gr.DrawImage(image, points, rect,
            GraphicsUnit.Pixel, attributes);
    }

    // Return the result.
    return bm;
}

The key is the ColorMatrix object. If a pixel initially has color components R, G, and B, this matrix makes its new components all equal to 0.299 * R + 0.587 * G + 0.114 * B. The result is a weighted average that produces a nice monochrome result. (You can set all of the matrix’s non-zero entries to 0.33f if you want to use a non-weighted average, but the weighted version produces a slightly better result.)

See the example Use an ImageAttributes object to adjust an image’s brightness in C# for additional details about how the drawing part works.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in graphics, image processing and tagged , , , , , , , , , , , , , . Bookmark the permalink.

One Response to Use an ImageAttributes object to convert an image to monochrome in C#

  1. Pingback: Use an ImageAttributes to convert an image to sepia tone

Leave a Reply

Your email address will not be published. Required fields are marked *