10

I'm having problem with lightening picture. I know I could do this by increasing/decreasing pixel's values, however it's not what I'm looking for. I've been told that there's a way that do not require to iterate through all pixels and that it is much faster. How to do this? Thanks.

Patryk
  • 3,042
  • 11
  • 41
  • 83

3 Answers3

10

To lighten an image, you can apply a white haze over it:

Bitmap bmp = new Bitmap(@"Image.png");
Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
alpha = 128
using (Graphics g = Graphics.FromImage(bmp)) {
    using (Brush cloud_brush = new SolidBrush(Color.FromArgb(alpha, Color.White))) {
        g.FillRectangle(cloud_brush, r);
    }
}
bmp.Save(@"Light.png");

The alpha value can range from 0 to 255. 0 means that the effect is completely transparent resulting in the original image. 255 means that the effect is completely opaque resulting in a solid white rectangle. Here, the effect is shown with 128.

Lightened Image (Before and After)

To darken an image, you can apply a black haze over it:

Bitmap bmp = new Bitmap(@"Image.png");
Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
alpha = 128
using (Graphics g = Graphics.FromImage(bmp)) {
    using (Brush cloud_brush = new SolidBrush(Color.FromArgb(alpha, Color.Black))) {
        g.FillRectangle(cloud_brush, r);
    }
}
bmp.Save(@"Dark.png");

The alpha value can range from 0 to 255. 0 means that the effect is completely transparent resulting in the original image. 255 means that the effect is completely opaque resulting in a solid black rectangle. Here, the effect is shown with 128.

Darkened Image (Before and After)

Mike F
  • 521
  • 5
  • 11
7

You use the ColorMatrix class in System.Drawing code to apply color transformations in a highly optimized way. Effects like brightness and contrast changes are directly supported. This web page shows you what the matrix should look like.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

To change a pictures brightness, you always have to change each pixel (if it's not a vector based graphic). iterating through the all pixels should be the only way I think. Maybe there is a framework which does the work for you. or you can look for a fast algorithm which combines a matrix of pixels to a subpixel. all in all there are many possibilitys. image processing is a complex topic.

you can find a few examples here: http://www.authorcode.com/making-image-editing-tool-in-c-brightness-of-an-image/

Tobias
  • 11
  • 3