2013-03-14 115 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);