【发布时间】:2013-03-02 18:18:24
【问题描述】:
在 .NET 中调整图像亮度对比度和伽玛的简单方法是什么
我会自己发布答案,以便稍后找到。
【问题讨论】:
标签: c# .net image image-processing brightness
在 .NET 中调整图像亮度对比度和伽玛的简单方法是什么
我会自己发布答案,以便稍后找到。
【问题讨论】:
标签: c# .net image image-processing brightness
c# 和 gdi+ 有一个简单的方法来控制绘制的颜色。 它基本上是一个 ColorMatrix。它是一个 5×5 矩阵,适用于 每种颜色(如果已设置)。调整亮度只是执行 转换颜色数据,对比度在 颜色。 Gamma 是一种完全不同的变换形式,但它包含在 在接受 ColorMatrix 的 ImageAttributes 中。
Bitmap originalImage;
Bitmap adjustedImage;
float brightness = 1.0f; // no change in brightness
float contrast = 2.0f; // twice the contrast
float gamma = 1.0f; // no change in gamma
float adjustedBrightness = brightness - 1.0f;
// create matrix that will brighten and contrast the image
float[][] ptsArray ={
new float[] {contrast, 0, 0, 0, 0}, // scale red
new float[] {0, contrast, 0, 0, 0}, // scale green
new float[] {0, 0, contrast, 0, 0}, // scale blue
new float[] {0, 0, 0, 1.0f, 0}, // don't scale alpha
new float[] {adjustedBrightness, adjustedBrightness, adjustedBrightness, 0, 1}};
ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.ClearColorMatrix();
imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
Graphics g = Graphics.FromImage(adjustedImage);
g.DrawImage(originalImage, new Rectangle(0,0,adjustedImage.Width,adjustedImage.Height)
,0,0,originalImage.Width,originalImage.Height,
GraphicsUnit.Pixel, imageAttributes);
【讨论】: