要動態更改PictureBox圖片的亮度、對比度或飽和度,可以使用以下步驟:
Bitmap bitmap = new Bitmap(pictureBox1.Image);
Bitmap newBitmap = new Bitmap(bitmap.Width, bitmap.Height);
float brightness = 0.1f; //亮度
float contrast = 1.5f; //對比度
float saturation = 1.5f; //飽和度
using (Graphics g = Graphics.FromImage(newBitmap))
{
ImageAttributes attributes = new ImageAttributes();
//亮度調整
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[] {brightness, brightness, brightness, 0, 1}
};
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
attributes.SetColorMatrix(colorMatrix);
//對比度調整
g.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, attributes);
//飽和度調整
ImageAttributes saturationAttributes = new ImageAttributes();
saturationAttributes.SetColorMatrix(new ColorMatrix
{
Matrix33 = saturation
});
g.DrawImage(newBitmap, new Rectangle(0, 0, newBitmap.Width, newBitmap.Height), 0, 0, newBitmap.Width, newBitmap.Height, GraphicsUnit.Pixel, saturationAttributes);
}
pictureBox1.Image = newBitmap;
通過以上步驟,可以動態更改PictureBox中圖片的亮度、對比度和飽和度。您可以根據需要調整brightness、contrast和saturation的值來實現不同的效果。