Other Posts in Image Editing

  1. Perlin Noise
  2. Fault Formation
  3. Cellular Textures
  4. Resizing an Image in C#
  5. Box Blur and Gaussian Blur... Sort of...
  6. Thermal Erosion
  7. Using Mid Point Displacement to Create Cracks
  8. Fluvial Erosion
  9. Creating Marble Like Textures Procedurally
  10. Procedural Textures and Dilation
  11. Converting Image to Black and White in C#
  12. Getting an HTML Based Color Palette from an Image in C#
  13. Adding Noise/Jitter to an Image in C#
  14. Creating Pixelated Images in C#
  15. Edge detection in C#
  16. Using Sin to Get What You Want... In C#...
  17. Noise Reduction of an Image in C# using Median Filters
  18. Image Dilation in C#
  19. Sepia Tone in C#
  20. Kuwahara Filter in C#
  21. Matrix Convolution Filters in C#
  22. Symmetric Nearest Neighbor in C#
  23. Bump Map Creation Using C#
  24. Normal Map Creation Using C#
  25. Creating Negative Images using C#
  26. Red, Blue, and Green Filters in C#
  27. Converting an Image to ASCII Art in C#
  28. Adjusting Brightness of an Image in C#
  29. Adding Noise to an Image in C#
  30. Adjusting the Gamma of an Image Using C#
  31. Adjusting Contrast of an Image in C#
  32. Drawing a Box With Rounded Corners in C#
  33. Anding Two Images Together Using C#
  34. Motion Detection in C#
  35. Creating Thermometer Chart in C#
  36. Colorizing a Black and White Image in C#
  37. Extracting an Icon From a File
  38. Setting the Pixel Format and Image Format of an Image in .Net
  39. Using Unsafe Code for Faster Image Manipulation
  40. Sobel Edge Detection and Laplace Edge Detection in C#

Adjusting Brightness of an Image in C#

4/17/2009

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.

   1: /// <summary>
   2: /// Adjusts the brightness
   3: /// </summary>
   4: /// <param name="Image">Image to change</param>
   5: /// <param name="Value">-255 to 255</param>
   6: /// <returns>A bitmap object</returns>
   7: public static Bitmap AdjustBrightness(Bitmap Image, int Value)
   8: {
   9:     float FinalValue = (float)Value / 255.0f;
  10:     ColorMatrix TempMatrix = new ColorMatrix();
  11:     TempMatrix.Matrix = new float[][]{
  12:                     new float[] {1, 0, 0, 0, 0},
  13:                     new float[] {0, 1, 0, 0, 0},
  14:                     new float[] {0, 0, 1, 0, 0},
  15:                     new float[] {0, 0, 0, 1, 0},
  16:                     new float[] {FinalValue, FinalValue, FinalValue, 1, 1}
  17:                 };
  18:     return TempMatrix.Apply(Image);
  19: }
  20:  
  21:  
  22: /*
  23: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
  24: 
  25: Permission is hereby granted, free of charge, to any person obtaining a copy
  26: of this software and associated documentation files (the "Software"), to deal
  27: in the Software without restriction, including without limitation the rights
  28: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  29: copies of the Software, and to permit persons to whom the Software is
  30: furnished to do so, subject to the following conditions:
  31: 
  32: The above copyright notice and this permission notice shall be included in
  33: all copies or substantial portions of the Software.
  34: 
  35: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  36: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  37: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  38: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  39: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  40: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  41: THE SOFTWARE.*/
  42:  
  43: #region Usings
  44: using System.Drawing;
  45: using System.Drawing.Imaging;
  46: #endregion
  47:  
  48: namespace Utilities.Media.Image
  49: {
  50:     /// <summary>
  51:     /// Helper class for setting up and applying a color matrix
  52:     /// </summary>
  53:     public class ColorMatrix
  54:     {
  55:         #region Constructor
  56:  
  57:         /// <summary>
  58:         /// Constructor
  59:         /// </summary>
  60:         public ColorMatrix()
  61:         {
  62:         }
  63:  
  64:         #endregion
  65:  
  66:         #region Properties
  67:  
  68:         /// <summary>
  69:         /// Matrix containing the values of the ColorMatrix
  70:         /// </summary>
  71:         public float[][] Matrix { get; set; }
  72:  
  73:         #endregion
  74:  
  75:         #region Public Functions
  76:  
  77:         /// <summary>
  78:         /// Applies the color matrix
  79:         /// </summary>
  80:         /// <param name="OriginalImage">Image sent in</param>
  81:         /// <returns>An image with the color matrix applied</returns>
  82:         public Bitmap Apply(Bitmap OriginalImage)
  83:         {
  84:             Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
  85:             using (Graphics NewGraphics = Graphics.FromImage(NewBitmap))
  86:             {
  87:                 System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(Matrix);
  88:                 using (ImageAttributes Attributes = new ImageAttributes())
  89:                 {
  90:                     Attributes.SetColorMatrix(NewColorMatrix);
  91:                     NewGraphics.DrawImage(OriginalImage,
  92:                         new System.Drawing.Rectangle(0, 0, OriginalImage.Width, OriginalImage.Height),
  93:                         0, 0, OriginalImage.Width, OriginalImage.Height,
  94:                         GraphicsUnit.Pixel,
  95:                         Attributes);
  96:                 }
  97:             }
  98:             return NewBitmap;
  99:         }
 100:  
 101:         #endregion
 102:     }
 103: }

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.



Comments