Making image editing tool in C#-Brightness of an image

Now next article of this series describes how to set the brightness of an image.
For this we can use concept of colormatrix class of system.Drawing namespace.ColorMattrix defines a 5 x 5 matrix that contains the coordinates for the RGBAW space.

Download Source Code
You can go msdn for understanding concept of color matrix

first design brightness tab like this layout

Image editor -Brightness of an image

write code on the trackbar_scroll event:

DomainUpDownBrightness.Text = TrackBarBrightness.Value.ToString();
float value = TrackBarBrightness.Value * 0.01f;

This line set the brightness value of image in domainUpDown control withrange -100 to 100.
And below we demine single type array in 5 x 5 matrix

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

Define colormatrix

ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

An ImageAttributes object maintains color and grayscale settings for five adjustment categories: default, bitmap, brush, pen, and text. For example, you can specify a color-adjustment matrix for the default category, a different color-adjustment matrix for the bitmap category, and still a different color-adjustment matrix for the pen category.

ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
Image _img = Img;
Graphics _g = default(Graphics);
Bitmap bm_dest = new Bitmap(Convert.ToInt32(_img.Width), Convert.ToInt32(_img.Height));
_g = Graphics.FromImage(bm_dest);
_g.DrawImage(_img, new Rectangle(0, 0, bm_dest.Width + 1, bm_dest.Height + 1), 0, 0, bm_dest.Width + 1, bm_dest.Height + 1, GraphicsUnit.Pixel, imageAttributes);
PictureBox1.Image = bm_dest;

Brightness of the an Image in the Image editor in C#
you can get brightness setting by above code

Other related articles of this series:

Making image editing tool in C#

making image editing tool in C#- Resize an image

Making image editing tool in C#- Crop an image

Making image editing tool in C#- Rotations of an image

4 thoughts on “Making image editing tool in C#-Brightness of an image”

  1.       your website has helped me a lot to carry out my project. its a superb website. thank u soooooooo much.

          Iam stuck with a problem. please help me.
    1) From your website i got the code for rotating the image. But the rotation is only for 90, 180…. how     to rotate an image by externally specifying the angle of rotation? like 10 degree, 21 degree, 3 degree………
    2)  how  to externally adjust the contrast and brightness simultaneously using scroll bar.
    3) how to convert an image to transparent .

Comments are closed.