【C#/WPF】调节图像的对比度(Contrast)
生活随笔
收集整理的這篇文章主要介紹了
【C#/WPF】调节图像的对比度(Contrast)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
關于對比度:
調節對比度直觀感受是,高對比度的圖像明暗關系更明顯,色彩更鮮艷;低對比度的圖像表面像是蒙上一層灰,色彩不鮮艷。
需求:
制作一個面板,一個滑動條,拖動滑動條可以修改目標圖片的對比度。
資料參考:
https://softwarebydefault.com/2013/04/20/image-contrast/
界面滑動條兩端的值是-30~30,默認處于中間位置0。已知目標圖像的Bitmap數據。
修改Bitmap的對比度。
將修改之后的Bitmap重新賦值給界面Image控件顯示。
/// <summary>
/// 調節對比度
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Contrast_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
// 滑動條是值是-30~30
// originalBitmap是目標圖像的Bitmap
int threshold = (int)e.NewValue;
Bitmap newBitmap = BitmapHelper.Contrast(originalBitmap, threshold);
// 重新給Image控件賦值新圖像
image.Source = SystemUtils.ConvertBitmapToBitmapImage(newBitmap);
}
下面調節圖像對比度的工具方法:
/// <summary>
/// 代碼來自:https://softwarebydefault.com/2013/04/20/image-contrast/
/// </summary>
public static class BitmapHelper
{
/// <summary>
/// 調節圖像的對比度
/// </summary>
/// <param name="sourceBitmap"></param>
/// <param name="threshold">閾值,通過該參數控制調節</param>
/// <returns></returns>
public static Bitmap Contrast(this Bitmap sourceBitmap, int threshold)
{
BitmapData sourceData = sourceBitmap.LockBits(new Rectangle(0, 0,
sourceBitmap.Width, sourceBitmap.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
byte[] pixelBuffer = new byte[sourceData.Stride * sourceData.Height];
Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length);
sourceBitmap.UnlockBits(sourceData);
double contrastLevel = Math.Pow((100.0 + threshold) / 100.0, 2);
double blue = 0;
double green = 0;
double red = 0;
for (int k = 0; k + 4 < pixelBuffer.Length; k += 4)
{
blue = ((((pixelBuffer[k] / 255.0) - 0.5) *
contrastLevel) + 0.5) * 255.0;
green = ((((pixelBuffer[k + 1] / 255.0) - 0.5) *
contrastLevel) + 0.5) * 255.0;
red = ((((pixelBuffer[k + 2] / 255.0) - 0.5) *
contrastLevel) + 0.5) * 255.0;
if (blue > 255)
{ blue = 255; }
else if (blue < 0)
{ blue = 0; }
if (green > 255)
{ green = 255; }
else if (green < 0)
{ green = 0; }
if (red > 255)
{ red = 255; }
else if (red < 0)
{ red = 0; }
pixelBuffer[k] = (byte)blue;
pixelBuffer[k + 1] = (byte)green;
pixelBuffer[k + 2] = (byte)red;
}
Bitmap resultBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height);
BitmapData resultData = resultBitmap.LockBits(new Rectangle(0, 0,
resultBitmap.Width, resultBitmap.Height),
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
Marshal.Copy(pixelBuffer, 0, resultData.Scan0, pixelBuffer.Length);
resultBitmap.UnlockBits(resultData);
return resultBitmap;
}
}
下面是將Bitmap轉換為BitmapImage的工具方法,以供WPF的Image控件使用圖像:
public static class SystemUtils
{ /// <summary> /// 轉換類型:Bitmap --> BitmapImage /// <summary> /// <returns></returns> public static BitmapImage ConvertBitmapToBitmapImage(Bitmap bitmap) { using (MemoryStream stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Png); stream.Position = 0; BitmapImage bi = new BitmapImage(); bi.BeginInit(); // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed." // Force the bitmap to load right now so we can dispose the stream. bi.CacheOption = BitmapCacheOption.OnLoad; bi.StreamSource = stream; bi.EndInit(); bi.Freeze(); return bi; } } }
測試效果如下:
另外,關于圖像的HSL(色相、飽和度、明度)的調節,可參考在下的另一篇博文:
http://www.cnblogs.com/guxin/p/csharp-wpf-adjust-image-hue-saturation-lightness-by-imagemagick.html
參考資料:
https://softwarebydefault.com/2013/04/20/image-contrast/
總結
以上是生活随笔為你收集整理的【C#/WPF】调节图像的对比度(Contrast)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 航母上112是什么意思?
- 下一篇: 怎么创建具有真实纹理的CG场景岩石?