Community discussion forum

Image Manipulation - Brightness and Contrast

This is a comment thread discussing Image Manipulation - Brightness and Contrast
  • 10 years ago

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

  • 3 years ago

    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












  • 3 years ago

    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();

            }

  • 2 years ago

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

  • 2 years ago

    Hi

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

    Thanks.

    Atan Kumar

  • 2 years ago
    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).








  • 7 months ago
    Not a bad attempt.. the output would look OK for most purposes but a 'true' image lighten/darken requires the adjustment to be made as a logarithmic function of the input channel. This reduces wash-out for images that already have bright/dark elements, and produces a more realistic result. For example if the adjustment factor was 20, then a black pixel might become (20, 20, 20) but a grey pixel might go from (128, 128, 128) to (138, 138, 138).
  • 6 months ago

    Here is some code written in VB.net but you should be able to convert it, For brightness : http://ess-image.com/Graphic/ImageProcessing/AdjustBrightness.aspx For Contrast (Gamma Correction) : http://ess-image.com/Graphic/Image_Processing/GammaCorrection.aspx I hope this helps

Post a reply

Enter your message below

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

Want to stay in touch with what's going on? Follow us on twitter!