Library tutorials & articles
Image Manipulation - Brightness and Contrast
By Mitch Dusina, published on 11 Sep 2007
Page 1 of 4
- Brightness - Introduction
- Brightness - The Algorithm
- Contrast - Introduction
- Contrast - The Algorithm
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:
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
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
Related articles
Related discussion
-
how to select multiple files at a time using Ctrl+select and upload the attachments in C# .Net 1.0?
by vasanta (0 replies)
-
Help please for student
by mandy130 (0 replies)
-
Loop help needed
by BKRoberts (5 replies)
-
Problem after strong naming an assembly
by rinkurathor1 (0 replies)
-
Regarding User control creation in C# .Net(Windows Application)
by porchelvi (0 replies)
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!
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).
Hi
I think this is code for brightness only... wat will be the contrast code...
Thanks.
Atan Kumar
hey I am not able to decrease brightness though I can increase it
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();
}
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
This thread is for discussions of Image Manipulation - Brightness and Contrast.