Dan Rigsby – Coding Up Style

Developer.Speaker.Blogger

How to grayout an image in .Net

Posted by Dan Rigsby on March 28th, 2008

Here is a handy method for making a grayscale version of an image.  I use this method a lot to grayout icons that I wish to show as disabled.  All you need to do is pass in the image and it will apply a grayscale over it.  The key to getting all of this to work is the ColorMatrix.  There is a detailed article about how this works on codeproject: http://www.codeproject.com/KB/GDI-plus/colormatrix.aspx.

public static void ApplyGrayscale(
    ref Image image)
{
    // Build color matrix set at 1/3
    ColorMatrix matrix = new ColorMatrix();
    matrix[0, 0] = 1/3f;
    matrix[0, 1] = 1/3f;
    matrix[0, 2] = 1/3f;
    matrix[1, 0] = 1/3f;
    matrix[1, 1] = 1/3f;
    matrix[1, 2] = 1/3f;
    matrix[2, 0] = 1/3f;
    matrix[2, 1] = 1/3f;
    matrix[2, 2] = 1/3f;

    // Create image attributes that will applied to the image
    ImageAttributes attributes = new ImageAttributes();
    attributes.SetColorMatrix(matrix);

    Graphics g = null;
    try
    {
        // Get the graphics object from the image
        g = Graphics.FromImage(image);

        // Redraw the image on the graphics object using the grayscale color matrix
        g.DrawImage(image,
            new Rectangle(0, 0, image.Width, image.Height),
            0,
            0,
            image.Width,
            image.Height,
            GraphicsUnit.Pixel,
            attributes);
    }
    finally
    {
        if (g != null)
        {
            g.Dispose();
        }
    }
}

 

Here are some sample before and after images (of my daughter Claire):

claire clairegray

 

Here is another example with a more typical line-of-business image:

disk_blue disk_bluegray

 

To make this easier to try out or use, I have wrapped the method into a executable as well.  You can download the source for this here: http://www.danrigsby.com/Files/Rigsby.ImageUtilities.Grayscale.zip

To run the program, compile the code and execute the exe file while passing in the source file name and the file name you want to save the new grayscaled image to, like this:

Rigsby.ImageUtilities.Grayscale.exe "c:\Claire.jpg" "c:\Claire-Gray.jpg"
 

9 Responses to “How to grayout an image in .Net”

  1. Jeff Moser Says:

    Nice! Sounds like a helpful class to add to our common framework. Now, where to put it since it depends on System.Drawing…

  2. Dan Rigsby Says:

    We have this in an ININ.Resources.dll file atm. System.Drawing is pretty standard. I would guess this could go into ININ.Common.dll without any issues. It would be good for web or windows clients.

  3. Dew Drop - March 30, 2008 | Alvin Ashcraft's Morning Dew Says:

    [...] How to Grayout an Image in .NET (Dan Rigsby) [...]

  4. yitzchok Says:

    “ht” missing from Link

  5. Finds of the Week - March 30, 2008 » Chinh Do Says:

    [...] Dan Rigsby demonstrates a technique to grayout an image in .Net. [...]

  6. Jonas Jämtberg Says:

    For a more human friendly grayscale conversion use: 30% of the red value, 59% of the green value, and 11% of the blue value (Wikipedia).

  7. C# 411 Says:

    Great function! Another one to try is the .NET built-in function in System.Windows.Forms:

    ControlPaint.DrawImageDisabled

  8. Mind Gravy » Blog Archive » links for 2008-04-02 Says:

    [...] Dan Rigsby » How to grayout an image in .Net (tags: .net graphics) [...]

  9. Wöchentliche Rundablage: ASP.NET MVC, Silverlight 2, .NET, RegEx, .NET, Icons, CSS, UI | Code-Inside Blog Says:

    [...] How to grayout an image in .Net [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>