How to lighten an image in C # without actually repeating all the pixels of the image?

I have a problem with the backlight. I know that I can do this by increasing / decreasing pixel values, however this is not what I am looking for. I was told that there is a method that does not require iteration over all pixels and that it is much faster. How to do it? Thanks.

+6
source share
3 answers

You use the ColorMatrix class in System.Drawing code to apply highly optimized color transformations. Direct support for effects such as brightness and contrast. This web page shows you what the matrix should look like.

+6
source

To lighten the image, you can apply a white mist on 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, which leads to the original image. 255 means that the effect is completely opaque, resulting in a solid white rectangle. Here the effect is shown at 128.

Illuminated image (before and after)

To darken an image, you can apply black haze on 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, which leads to the original image. 255 means that the effect is completely opaque, resulting in a solid black rectangle. Here the effect is shown at 128.

Dark image (before and after)

+5
source

To change the brightness of the images, you always need to change each pixel (if this is not a vector image). Iterating through all the pixels should be the only way I think. Perhaps there is a structure that does the work for you. or you can find a quick algorithm that combines a matrix of pixels into a subpixel. overall there are many possibilities. Image processing is a complex topic.

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

0
source

Source: https://habr.com/ru/post/944878/


All Articles