Search

Adjust the Contrast of an Image in C#

Adjusting the contrast of an image in C# is an easy operation because it can be done on a single pixel at a time. We only need to know 2 formulas to calculate a new pixel color based on an old pixel color, and they’re very easy to implement.

contrast = ((100.0 + T) / 100.0)2

newRed = ( ( ( (oldRed / 255.0) – 0.5) * contrast) + 0.5) * 255.0

Here’s a simple function that implements this formula for each pixel in a Bitmap object.

Results of Contrast Adjustment

Here is a sample image.

And here is that same image with the contrast adjusted to -50.

And here is that same image with the contrast adjusted up to 50.

How it Works

This block of code iterates over the width and height of a locked Bitmap object looking at each pixel one at a time. For each pixel it calculates a new Color object based on the formula provided above. You call this function with a threshold between -100 and 100.

Categories

About Efundies

The goal of efundies.com is to provide the Internet with a collection of fun, free, electronics projects and advice.
Jason Bauer is an owner and programmer for Portforward.com. He's allergic to twitter and facebook, but you can find more of his articles in the Guides section.