This content is not currently approved and is visible here for review only.

Library tutorials & articles

Image Manipulation - Brightness and Contrast

Brightness - Introduction

The Brightness of an image may be thought of as the overall luminance of every pixel.  To increase the brightness, all that needs to be done is to add the Adjustment value to every color channel.  The only difference in darkening an image is you subtract the value.  Brightness adjustment is used to lighten dark photographs or darken bright ones.  A "completely brightened image" is simply all white pixels while a "completely darkened image" is all black.

The general algorithm is represented by:
If Brighten = True
New_Value = Old_Value + Adjustment_Amount
Else
New_Value = Old_Value - Adjustment_Amount

If New_Value < Value_Minimum
New_Value = Value_Minimum
If New_Value > Value_Maximum
New_Value = Value_Maximum

Using this algorithm, here is our Earth Brightened by 32, Unadjusted, and Darkened by 32:


Notice that in the brightened image, the black border around the Earth changed to a dark grey.  But in the darkened version, the black border remained black.  This behavior results because there is nothing darker than black.  The opposite is also true, there is nothing lighter than white.

Earth Image shot by NASA, provided by FreeImages.co.uk
Adjustments performed by my image program, Colithium Graphics

Comments

  1. 11 Sep 2007 at 04:55

    I have just returned from an extended absence from this forum so I realize this is rather late.

    JohnMAndre:
    1) I'm not sure how that even got in there, I'll remove it at once.
    2) I was almost certain I had inclueded the code for the SupportFunctions, my mistake.  I'll correct it in the article

    MD2020:
    GetPixel/SetPixel is the slowest way of doing image processing in .NET, not using unsafe code.  The unsafe method is quiet fast actually.  As for using a ColorMatrix, it is fast as well.  However at the time of writing this article my research showed that a ColorMatrix was slightly slower than unsafe code (noticable on large images or slow machines).  However as of this posting, I have found mixed arguments.  Either way, they have similar speeds.  I'll try to run some benchmarks to verify the correct answer (or if someone would like to do it and post their results here).








  2. 05 Jun 2007 at 13:25

    Hi

    I think this is code for brightness only... wat will be the contrast code...

    Thanks.

    Atan Kumar

  3. 30 Nov 2006 at 18:15

    hey I am not able to decrease brightness though I can increase it

  4. 11 Sep 2006 at 22:13

    Locking the image and using an unsafe block is the slowest way to do image processing in GDI+. There is an even faster way to do this. Use a color Matrix. Here is the code.

     

    public static void AdjustBrightnessMatrix(Bitmap img, int value)

            {

                if (value == 0) // No change, so just return

                    return;

     

                float sb = (float)value / 255F;

                float[][] colorMatrixElements =

                      {

                            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[] {sb, sb, sb, 1, 1}

                      };

     

                ColorMatrix cm = new ColorMatrix(colorMatrixElements);

                ImageAttributes imgattr = new ImageAttributes();

                Rectangle rc = new Rectangle(0, 0, img.Width, img.Height);

                Graphics g = Graphics.FromImage(img);

                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                imgattr.SetColorMatrix(cm);

                g.DrawImage(img, rc, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgattr);

               

                //Clean everything up

                imgattr.Dispose();

                g.Dispose();

            }

  5. 30 Aug 2006 at 09:12

    A fine article, but there are two small points you might want to address:

    1) The code is in C# but there is a VB line-continuation character. Unless you remove it, the code will not compile.

    2) You call SupportFunctions.BytesPerPixel but you don't list the code for that. Therefore, the code will still not compile.

    Of course, the reader can always research these problems and find solutions but it would be nice to have all the answers in one place.

    Best Regards,
    John












  6. 01 Jan 1999 at 00:00

    This thread is for discussions of Image Manipulation - Brightness and Contrast.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related podcasts

  • Looking into the C# Crystal Ball with Charlie Calvert and Bill Wagner

    One of the most exciting announcements from PDC was the news about C# 4.0 and Visual Studio 2010. With all the excitement and discussion throughout the event about these new developer tools, we reached out to two experts in the fields. Charlie Calvert and Bill Wagner sat down with Keith and Woody...

Events coming up

  • Dec 6

    Developing AJAX Web Applications with Castle Monorail

    London, United Kingdom

    Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!