要动态更改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的值来实现不同的效果。