If you need to implement color grading in your image editing application, this article is going to give you a simple yet useful solution. Particularly, you will learn how to adjust an image’s contrast, brightness and gamma in C# following a few simple steps.
Contrast), brightness, and gamma are popular attributes that are used to tune images. Different values of these attributes are set to control and enhance the appearance of the images. In this article, we will show you how to adjust the contrast, brightness, and gamma of an image programmatically in C#. You can utilize this to implement image editing capabilities from within your .NET applications.
- C# Image Editing API
- Adjust Contrast of an Image in C#
- Adjust Brightness of an Image in C#
- Modify Gamma of an Image in C#
Adjust Image Contrast, Brightness, and Gamma in C# - API Installation
Aspose.Imaging for .NET is a powerful and feature-rich image processing API for .NET/.NET Standard platforms. It supports the manipulation of a wide range of image formats without writing complex code. We will use this API to adjust the contrast, brightness, and gamma of images in C#. You can download the API or install it into your .NET applications using NuGet.
PM> Install-Package Aspose.Imaging
Adjust Contrast of an Image in C#
The contrast is defined as the difference in pixel intensity. Its value is adjusted to make the objects in an image more visible and distinguishable. With high contrast, every object in the image looks very clear just like the photographs taken in the sunlight. Whereas, with low contrast, you hardly distinguish the objects just like in the foggy weather.
Let’s check out how to adjust the contrast of an image in C# using Aspose.Imaging for .NET.
- First, load the image using Image class.
- Then, cast the object to RasterImage type.
- Cache image if it isn’t using RasterImage.CacheData() method.
- Adjust contrast in range [-100, 100] using RasterImage.AdjustContrast(float) method.
- Finally, save the resultant image using RasterImage.Save(string) method.
The following code sample shows how to adjust the contrast of an image in C#.
The following is the comparison of input and output images after increasing the contrast value.
Adjust Brightness of an Image in C#
The brightness attribute usually controls the visibility of the objects in an image. Its value is adjusted to increase or decrease the darkness. The following are the steps to modify the brightness of an image in C#.
- First, load the image using Image class.
- Cast object to RasterImage type.
- Cache image if it isn’t using RasterImage.CacheData() method.
- Adjust the brightness of the image using RasterImage.AdjustBrightness(float) method.
- Finally, save the resultant image using RasterImage.Save(string) method.
The following code sample shows how to adjust the brightness of an image in C#.
The following is the comparison of input and output images after adjusting brightness.
Modify Gamma of an Image in C#
The gamma attribute is used to modify the ratio of red, green, and blue colors in an image. Moreover, it also modifies the brightness of the image. The following are the steps to change the gamma value of an image in C#.
- First, load the image using Image class.
- Cast object to RasterImage type.
- Cache image if it isn’t using RasterImage.CacheData() method.
- Change gamma of the image using RasterImage.AdjustGamma(float, float, float) method.
- Finally, save the resultant image using RasterImage.Save(string) method.
The following code sample shows how to adjust the gamma of an image in C#.
The following is the comparison of input and output images after adjusting gamma value.
Free License to Adjust Image’s Contrast and Brightness
You can get a free temporary license and adjust the image’s contrast, brightness, and gamma without evaluation limitations.
Conclusion
In this article, you have learned how to adjust the contrast, brightness, and gamma of images in C#. Furthermore, the code samples have demonstrated how an image looks after modifying the contrast, brightness, and gamma. In case you want to explore more about .NET image processing API, visit documentation. Also, download the complete source code samples of the API from GitHub. If you would have any questions or queries, reach us at our forum.