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