[函数名称]
图像对比度调整函数ContrastAdjustProcess(WriteableBitmap
src, doublecontrastValue)
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2QvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcFzZU9EeR5mYspESWZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39zNxgjNzUTNwEzNwQDM1EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
[函数代码]
///<summary>
///
Contrast adjust process.
///</summary>
///<param
name="src">Source image.</param>
name="contrastValue">Contrast value, from -1 to 1.</param>
///<returns></returns>
publicstaticWriteableBitmap
ContrastAdjustProcess(WriteableBitmap src,double
contrastValue)////6对比度调整
{
if(src!=null
)
{
int
w = src.PixelWidth;
h = src.PixelHeight;
WriteableBitmap
contrastImage =newWriteableBitmap(w,h);
byte[]
temp = src.PixelBuffer.ToArray();
for
(int i = 0; i < temp.Length; i += 4)
temp[i] =Convert.ToByte((((temp[i]
- 127.5) * contrastValue + temp[i]) > 255 ? 255 : ((temp[i] - 127.5) * contrastValue + temp[i])) < 0 ? 0 : (((temp[i] - 127.5) * contrastValue + temp[i]) > 255 ? 255 : ((temp[i] - 127.5) * contrastValue + temp[i])));
temp[i + 1] =Convert.ToByte((((temp[i
+ 1] - 127.5) * contrastValue + temp[i + 1]) > 255 ? 255 : ((temp[i + 1] - 127.5) * contrastValue + temp[i + 1])) < 0 ? 0 : (((temp[i + 1] - 127.5) * contrastValue + temp[i + 1]) > 255 ? 255 : ((temp[i + 1] - 127.5) * contrastValue + temp[i + 1])));
temp[i + 2] =Convert.ToByte((((temp[i
+ 2] - 127.5) * contrastValue + temp[i + 2]) > 255 ? 255 : ((temp[i + 2] - 127.5) * contrastValue + temp[i + 2])) < 0 ? 0 : (((temp[i + 2] - 127.5) * contrastValue + temp[i + 2]) > 255 ? 255 : ((temp[i + 2] - 127.5) * contrastValue + temp[i + 2])));
}
Stream
sTemp = contrastImage.PixelBuffer.AsStream();
sTemp.Seek(0,SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return
contrastImage;
else
returnnull;
}
}