Darken an Image in C#

In order to darken an image in C# you need to look at each pixel one at a time and decrease it’s reg, green, and blue values. There are a few ways to do this.

  • The easiest way is just just decrement the reg, green, and blue by a fixed percentage, and limit their minimum value to 0.
  • The hard way is to deal with the hue shift that can occur when one of the values “clips” at the 0 mark.

This article shows you how to do the easy way.

The Reslts

Here is a test image.


And here are the results of setting darkenAmount to 0.5.


How it Works

This block of code iterates over the images width and height looking at each pixel. For each pixel it calls the function DarkenColor. DarkenColor calculates the amount of “darkenning” to apply for each of the 3 RGB elements and returns a new Color object.

The LockedBits Class

You need to have the LockedBits class in your project in order to use this code. You can learn about it in our How to Load an Image in C# guide.

Other C# Image Processing Guides

You might be interested in some of our other C# Image Processing 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.