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.

The LockedBitmap Class

You need to have the LockedBitmap class to make this example work. You can read about it on the How to Open an Image in C# guide.

Other C# Image Processing Guides

Be sure to check out some of our other C# Image Processing Guides for related guides.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.