The Levels effect allows you fine control over brightness and contrast.

Well defined images span an entire range of color intensities. However it is common to find images that do not. If a photo has been overexposed it will be too bright - there will be few colors at the low ends of intensity and many at the high end. Similarly if a photograph has been underexposed it will be very dark - all the colors will be at the low end of the range and virtually none at the high end.

The Levels effect allows you fine control over brightness and contrast to let you correct this kind of problem. The basic method of adjustment is to set the black and white points on the input image. Normally the black point will be at 0 and the white point at 255. This simply means that black is represented by the value 0 and white is represented by the value 255.

However if an image is too dark there may be no pixels at all with a value of 255. In this case what was white on the original image might be represented by a value of only 200. By setting the white input point to 200 and then applying the effect, the levels in between will be stretched to try and restore balance to the image. A similar operation setting the black input point would apply if an image was too light.

As well as being able to specify input points you can also specify output points. This lets you tell the effect what value should be regarded as white and black on the final output image.

The levels effect is often used in conjunction with an image histogram so that the exact representation of different color levels can be seen in the image.

Syntax

[C#]

static void Levels(Bitmap bitmap, double blackInput, double whiteInput, double blackOutput, double whiteOutput);

[Visual Basic]

Shared Sub Levels(bitmap As Bitmap, blackInput As Double, whiteInput As Double, blackOutput As Double, whiteOutput As Double)
Params
Name Description
bitmap The bitmap to process
blackInput The level in the current image that should be regarded as black. This value is a percentage so it should range between 0 and 100. A sensible default would be 0.
whiteInput The level in the current image that should be regarded as white. This value is a percentage so it should range between 0 and 100. A sensible default would be 100.
blackOutput The level in the final image that should be regarded as black. This value is a percentage so it should range between 0 and 100. A sensible default would be 0.
whiteOutput The level in the final image that should be regarded as white. This value is a percentage so it should range between 0 and 100. A sensible default would be 100.
Notes

None.

Example

[C#]using (Bitmap bm = (Bitmap)Bitmap.FromFile(Server.MapPath("rez/sora-sagano-8sOZJ8JF0S8-unsplash.jpg"))) {
  Effects.Levels(bm, 0, 80, 0, 100);
  bm.Save(Server.MapPath("IG8_Effects_Levels1.jpg"));
}
using (Bitmap bm = (Bitmap)Bitmap.FromFile(Server.MapPath("rez/sora-sagano-8sOZJ8JF0S8-unsplash.jpg"))) {
  Effects.Levels(bm, 20, 100, 0, 100);
  bm.Save(Server.MapPath("IG8_Effects_Levels2.jpg"));
}
using (Bitmap bm = (Bitmap)Bitmap.FromFile(Server.MapPath("rez/sora-sagano-8sOZJ8JF0S8-unsplash.jpg"))) {
  Effects.Levels(bm, 0, 100, 0, 80);
  bm.Save(Server.MapPath("IG8_Effects_Levels3.jpg"));
}


Here we apply a Levels edge detection filter using different radii. Ouput files are shown below.


sora-sagano-8sOZJ8JF0S8-unsplash.jpg


IG8_Effects_Levels1.jpg


IG8_Effects_Levels2.jpg


IG8_Effects_Levels3.jpg