How to make image editor tool in C#: Brightness of image


My last three articles discussed how to load an image into a Windows form, how to resize that image and how to crop an image; see:

Part1: Open an image

Part2: Resizing image

Part3: Cropping image

You can review the previous articles in this series.

This article of the series describes how to set the brightness of an image. 

For this we can use the ColorMatrix class of the System.Drawing.Imaging namespace. The ColorMatrix class defines a 5 x 5 matrix that contains the coordinates for the RGBAW space. 

You can go to the MSDN for understanding the ColorMatrix class.

http://msdn.microsoft.com/en-us/library/system.drawing.imaging.colormatrix.aspx

http://msdn.microsoft.com/en-us/library/system.drawing.imaging.colormatrix(v=VS.71).aspx

First design the brightness tab like in this layout:

7.jpg

Open an image in application (see this).

8.jpg

Write code for the trackbar_scroll event:

DomainUpDownBrightness.Text = TrackBarBrightness.Value.ToString();

float value = TrackBarBrightness.Value * 0.01f;   

This line sets the brightness value of an image in domainUpDown control with range -100 to 100.


And in the following we define a single type array in a 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;

            //PictureBox1.Image

            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;

You can get brightness setting using the code above.

9.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all