2013-03-14 103 views
6

什么是一个简单的方法来调整.NET图像的亮度对比度,伽玛调整图像

将发布自己的答案以后发现它的亮度对比度和伽玛。

回答

20

我已经找到了一个很好的和简单的例子here

C#和GDI +有一个简单的方法来控制要绘制的颜色。 它基本上是一个ColorMatrix。这是一个5×5矩阵,如果已设置,则应用于每种颜色的 。调整亮度只是执行 翻译上的颜色数据,以及对比度上 颜色进行规模。伽玛是一个完全不同的形式变换,但它包含在其中接受嘉洛斯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);