Adjusting Brightness of an Image in C#
I've been on another image processing kick... I don't think I'll be happy until I've added every filter out there. Anyway, this time I'm going to show you how to modify the brightness of an image. Brightness can really be thought of as the amount of light that something reflects. The brighter the item the more light it reflects, the dimmer the item the less light it reflects. And since we're dealing with the RGB color space, we can even calculate the brightness of a pixel by simply taking the average of the R, G, and B channels (with a higher number being brighter).
Since a higher number means that the pixel is brighter, all we need to do is add a fixed number to R, G, and B to make a pixel brighter (or subtract the value to make it darker). That sounds pretty easy but I don't really want to loop through each pixel myself. So how do we modify each pixel? ColorMatrix.
/// <summary>
/// Adjusts the brightness
/// </summary>
/// <param name="Image">Image to change</param>
/// <param name="Value">-255 to 255</param>
/// <returns>A bitmap object</returns>
public static Bitmap AdjustBrightness(Bitmap Image, int Value)
{
float FinalValue = (float)Value / 255.0f;
ColorMatrix TempMatrix = new ColorMatrix();
TempMatrix.Matrix = new float\[\]\[\]{
new float\[\] {1, 0, 0, 0, 0},
new float\[\] {0, 1, 0, 0, 0},
new float\[\] {0, 0, 1, 0, 0},
new float\[\] {0, 0, 0, 1, 0},
new float\[\] {FinalValue, FinalValue, FinalValue, 1, 1}
};
return TempMatrix.Apply(Image);
}
/\*
Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.\*/
#region Usings
using System.Drawing;
using System.Drawing.Imaging;
#endregion
namespace Utilities.Media.Image
{
/// <summary>
/// Helper class for setting up and applying a color matrix
/// </summary>
public class ColorMatrix
{
#region Constructor
/// <summary>
/// Constructor
/// </summary>
public ColorMatrix()
{
}
#endregion
#region Properties
/// <summary>
/// Matrix containing the values of the ColorMatrix
/// </summary>
public float\[\]\[\] Matrix { get; set; }
#endregion
#region Public Functions
/// <summary>
/// Applies the color matrix
/// </summary>
/// <param name="OriginalImage">Image sent in</param>
/// <returns>An image with the color matrix applied</returns>
public Bitmap Apply(Bitmap OriginalImage)
{
Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
using (Graphics NewGraphics = Graphics.FromImage(NewBitmap))
{
System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(Matrix);
using (ImageAttributes Attributes = new ImageAttributes())
{
Attributes.SetColorMatrix(NewColorMatrix);
NewGraphics.DrawImage(OriginalImage,
new System.Drawing.Rectangle(0, 0, OriginalImage.Width, OriginalImage.Height),
0, 0, OriginalImage.Width, OriginalImage.Height,
GraphicsUnit.Pixel,
Attributes);
}
}
return NewBitmap;
}
#endregion
}
}
The code above takes in an image and an integer between -255 and 255. In turn it uses a color matrix to modify the brightness of the image. You'll notice that the R, G, and B channels are really unchanged, with only the last row containing our value. In turn we get back a new modified image. That's all there is to it. So give it a try, leave feedback, and happy coding.
Sign up for More Posts
I have no idea why you'd put yourself through that but if you'd like more posts from me then how about using my RSS feed? Take a look.