Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
 
 
DZone MVB

Links

Social

How to create grayscale images on .NET

In this example we will use simple color recalculation method to grayscale images. For each pixel on image we will calculate "average color" that we write back on image. Average color is sum of current color's red, green and blue channels integer value divided by three. This value is assigned to all three channels for current pixel.

Color picture of rabbit on crime
Colored picture of rabbit commiting
the crime.

Althoug grayscaled she is still on action
Although grayscaled she is still on action.

The cool thing about this rabbit is her colors - black, white and gray. As you can see rabbit colors are almost the same on both pictures. Okay, let's see the code.


public Bitmap GrayScale(Bitmap Bmp)
{
    int rgb;
    Color c;
   
    for (int y = 0; y <Bmp.Height; y++)
        for (int x = 0; x <Bmp.Width; x++)
        {
            c = Bmp.GetPixel(x, y);
            rgb = (int)((c.R + c.G + c.B) / 3);
            Bmp.SetPixel(x, y, Color.FromArgb(rgb, rgb, rgb));
        }
    return Bmp;
}

Some notes about this code. It is also possible to calculate grayscale colors for channels differently. Instead of average of channels colors we can also use:

  • maximum of channel values,
  • minimum of channel values,
  • custom divider.

It depends purely on context what one may need. Channel values maximum is good for pictures that are little bit too dark. Minimum is good for pictures that are little bit too bright. Custom divider may find usage in applications where user may want to change brightness manually. Current example by itself is goof for original pictures that are okay enough to publish them.

I know there are faster methods available for grayscaling. These methods are usually based on unmanaged code. This example here is slower but it is managed code you can use safely in your applications.


kick it on DotNetKicks.com pimp it Progg it Shout it
Posted: Oct 23 2007, 12:14 AM by DigiMortal | with 7 comment(s) |
Filed under: , ,

Comments

Richard said:

The fastest method in pure managed code is to use a ColorMatrix:

www.codeproject.com/.../colormatrix.asp

# October 23, 2007 2:10 PM

Ramon Smits said:

Or use unsafe code by locking the bitmap object and retrieving the pointer in memory. The colormatrix through gdi is the fastest as this also can be done accellerated.

By the way... you are doing a very rude color to grey conversion. You should weight the color components differently.

# October 24, 2007 4:57 AM

mm said:

how to 8-bit bmp to 8-bit jpg??thanks!

# October 25, 2007 3:33 AM

DotNetBurner - ASP.net said:

DotNetBurner - burning hot .net content

# July 7, 2009 2:54 PM

PimpThisBlog.com said:

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# July 7, 2009 2:56 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# July 7, 2009 2:58 PM

progg.ru said:

Thank you for submitting this cool story - Trackback from progg.ru

# July 7, 2009 3:01 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)