17

What is an easy way to adjust brightness, contrast and gamma of an image in .NET?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
VladL
  • 12,769
  • 10
  • 63
  • 83

2 Answers2

33

c# and gdi+ have a simple way to control the colors that are drawn. It’s basically a ColorMatrix. It’s a 5×5 matrix that is applied to each color if it is set. Adjusting brightness is just performing a translate on the color data, and contrast is performing a scale on the color. Gamma is a whole different form of transform, but it’s included in ImageAttributes which accepts the ColorMatrix.

Bitmap originalImage;
Bitmap adjustedImage;
float brightness = 1.0f; // no change in brightness
float contrast = 2.0f; // twice the contrast
float gamma = 1.0f; // no change in gamma

float adjustedBrightness = brightness - 1.0f;
// create matrix that will brighten and contrast the image
float[][] ptsArray ={
        new float[] {contrast, 0, 0, 0, 0}, // scale red
        new float[] {0, contrast, 0, 0, 0}, // scale green
        new float[] {0, 0, contrast, 0, 0}, // scale blue
        new float[] {0, 0, 0, 1.0f, 0}, // don't scale alpha
        new float[] {adjustedBrightness, adjustedBrightness, adjustedBrightness, 0, 1}};

ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.ClearColorMatrix();
imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
Graphics g = Graphics.FromImage(adjustedImage);
g.DrawImage(originalImage, new Rectangle(0,0,adjustedImage.Width,adjustedImage.Height)
    ,0,0,originalImage.Width,originalImage.Height,
    GraphicsUnit.Pixel, imageAttributes);
VladL
  • 12,769
  • 10
  • 63
  • 83
0

Building on the answer from VladL

Bitmap originalImage = (Bitmap)myPictureBox.Image; // my application has a Winform with a PictureBox to display an image and is defined outside of this code block.
Bitmap adjustedImage = new Bitmap(myPictureBox.Image.Width, myPictureBox.Image.Height); // Created an instance of Bitmap with the same dimensions as myOriginalImage

float brightness = 1.0f; // no change in brightness
float contrast = 2.0f; // twice the contrast
float gamma = 1.0f; // no change in gamma

float adjustedBrightness = brightness - 1.0f;
// create matrix that will brighten and contrast the image
float[][] ptsArray ={
    new float[] {contrast, 0, 0, 0, 0}, // scale red
    new float[] {0, contrast, 0, 0, 0}, // scale green
    new float[] {0, 0, contrast, 0, 0}, // scale blue
    new float[] {0, 0, 0, 1.0f, 0}, // don't scale alpha
    new float[] {adjustedBrightness, adjustedBrightness, 
adjustedBrightness, 0, 1}};

ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.ClearColorMatrix();
imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), 
ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
Graphics g = Graphics.FromImage(adjustedImage);

g.DrawImage(originalImage, new Rectangle(0,0,adjustedImage.Width,adjustedImage.Height),0,0,originalImage.Width,originalImage.Height,GraphicsUnit.Pixel, imageAttributes);

// Now display the adjusted image in Winform PictureBox
myPictureBox.Image = adjustedImage;
this.Refresh();
JC5
  • 1
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 01 '23 at 09:24