Converting Image to Black and White in C#

Ok, I lied about no code this week... I swear that one of these days I'll run out of bits of code to hand out, but anyway. Have you ever seen the various picture galleries out there that do a nice rollover effect where every image is in black and white until you roll over it. Thus you get a nice highlighting effect where the one you're on is color. You might think that you'd need to upload the two images but in reality, all you need to do is convert the one image to black and white. And without further ado, here's the code to accomplish it:

        public static void ConvertBlackAndWhite(string FileName, string NewFileName)
        {
            System.Drawing.Image TempImage = System.Drawing.Image.FromFile(FileName);
            System.Drawing.Imaging.ImageFormat ImageFormat = TempImage.RawFormat;
            System.Drawing.Bitmap TempBitmap = new System.Drawing.Bitmap(TempImage, TempImage.Width, TempImage.Height);

            System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
            System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
            float[][] FloatColorMatrix ={
                    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}
                };

            System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(FloatColorMatrix);
            System.Drawing.Imaging.ImageAttributes Attributes = new System.Drawing.Imaging.ImageAttributes();
            Attributes.SetColorMatrix(NewColorMatrix);
            NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, System.Drawing.GraphicsUnit.Pixel, Attributes);
            NewGraphics.Dispose();
            NewBitmap.Save(NewFileName, ImageFormat);
        }

All this code does, is opens a single image that you specify, sets up a color matrix, tells GDI to convert the image using said color matrix, and writing it back out to a different file that you specify. That's all it's doing. However, there's one portion that might not be 100% clear if you're looking at it: What the values in the color matrix mean.

A color matrix contains 5 different arrays. The first deals with the resultant red color, second array determines the resultant green, third determines blue, and the fourth determines alpha. The fifth array is a special translation array, basically deals with intensity of the colors. And each of those arrays contains red, blue, green, and alpha values in that order with values normally going from 0 to 1 (although you can go outside that range). So a default matrix would look like this:

            float[][] FloatColorMatrix ={
                    new float[] {1, 0, 0, 0, 0},
                    new float[] {0, 1, 0, 0, 0},
                    new float[] {0, 0, 1, 0, 0},
                    new float[] {0, 0, 0, 1, 0},
                    new float[] {0, 0, 0, 0, 1}
                };

This would give us exactly what we put in as the original image. What we're doing in the code above, is taking the average of red, green, and blue at different amounts in order to get the appropriate grey scale. The reason we're not simply using using .5 for all of them is actually due to the sensitivity of the human eye. Basically if you want to read about it, I'd suggest going here: GrayScale.

Anyway, hopefully that explains the only nonobvious portion of the the code and will hopefully fit your needs. So copy it, try it out, leave feedback, and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 8/29/2008 at 2:18 PM
Tags: , ,
Categories: ASP.Net | C# | Web Design
Post Information: Permalink | Comments (1) | Post RSSRSS comment feed

Comments

John United States

Friday, December 18, 2009 9:59 PM

John

Wonderful contribution!  Needed a quick way to make a black-and-white version of an image so I didn't have to call up the designer and this code did exactly what I needed.

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading