Click here to Skip to main content
15,884,584 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hey all!

I have to create an image processing program using C#. I'm trying to add a code which adjusts the brightness using a track bar. When I move the slider of the track bar to the right, the code works fine and the picture becomes brighter, but when I move the slider to the left, the code doesn't return to its previous state, instead it the picture become brighter until it becomes completely white. I don't know what the error is.

This is my code:
private void trackBar1_Scroll(object sender, EventArgs e)
       {
               label2.Text = trackBar1.Value.ToString();
               pictureBox1.Image = AdjustBrightness((Bitmap)pictureBox1.Image, trackBar1.Value);

       }

       public static Bitmap AdjustBrightness(Bitmap Image, int Value)
       {

           Bitmap TempBitmap = Image;

           Bitmap NewBitmap = new Bitmap(TempBitmap.Width, TempBitmap.Height);

          Graphics NewGraphics = Graphics.FromImage(NewBitmap);

          float FinalValue = (float)Value / 255.0f;

           float[][] FloatColorMatrix ={

                    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[] {FinalValue, FinalValue, FinalValue, 1, 1}
                };

           ColorMatrix NewColorMatrix = new ColorMatrix(FloatColorMatrix);

           ImageAttributes Attributes = new ImageAttributes();

           Attributes.SetColorMatrix(NewColorMatrix);

           NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes);

           Attributes.Dispose();

           NewGraphics.Dispose();

           return NewBitmap;
       }


I don't get any errors or warnings when I run the code, but the only problem is that I can increase the brightness but I am not able to decrease the image.

Can anyone please help me on this? Am I missing a line of code? Or is it something I have to adjust from the track bar properties?

Thaaank you :D
Posted
Comments
Dr.Walt Fair, PE 20-Aug-11 21:20pm    
What values does your TrackBar give? How would the brightness decrease?
AmeeR-89 22-Aug-11 9:05am    
It gives me the value of the slider when I move it from left to right. The min is 0 and max is 100. The brightness should decrease when I move the slider to the left but it doesn't do that.
Member 14202540 29-Mar-19 7:41am    
Any solution for this

1 solution

Rather than doing it that way, try keeping a reference image instead:
C#
Bitmap myImage;
...
myImage = (Bitmap)pictureBox1.Image;
...
private void trackBar1_Scroll(object sender, EventArgs e)
{
        label2.Text = trackBar1.Value.ToString();
        pictureBox1.Image = AdjustBrightness(myImage, trackBar1.Value);

}
The problem probably is that you are setting a new, higher brightness on an image every time you move the slider, and throwing away the current one. Next time, you add brightness to the image you made brighter last time! So every time you slide, you increase the brightness, regardless of which way you move the slider!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900