【C#/WPF】调节图像的对比度(Contrast)
關(guān)于對(duì)比度:
調(diào)節(jié)對(duì)比度直觀感受是,高對(duì)比度的圖像明暗關(guān)系更明顯,色彩更鮮艷;低對(duì)比度的圖像表面像是蒙上一層灰,色彩不鮮艷。
需求:
制作一個(gè)面板,一個(gè)滑動(dòng)條,拖動(dòng)滑動(dòng)條可以修改目標(biāo)圖片的對(duì)比度。
資料參考:
https://softwarebydefault.com/2013/04/20/image-contrast/
界面滑動(dòng)條兩端的值是-30~30,默認(rèn)處于中間位置0。已知目標(biāo)圖像的Bitmap數(shù)據(jù)。
修改Bitmap的對(duì)比度。
將修改之后的Bitmap重新賦值給界面Image控件顯示。
/// <summary>
/// 調(diào)節(jié)對(duì)比度
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Contrast_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
// 滑動(dòng)條是值是-30~30
// originalBitmap是目標(biāo)圖像的Bitmap
int threshold = (int)e.NewValue;
Bitmap newBitmap = BitmapHelper.Contrast(originalBitmap, threshold);
// 重新給Image控件賦值新圖像
image.Source = SystemUtils.ConvertBitmapToBitmapImage(newBitmap);
}
下面調(diào)節(jié)圖像對(duì)比度的工具方法:
/// <summary>
/// 代碼來(lái)自:https://softwarebydefault.com/2013/04/20/image-contrast/
/// </summary>
public static class BitmapHelper
{
/// <summary>
/// 調(diào)節(jié)圖像的對(duì)比度
/// </summary>
/// <param name="sourceBitmap"></param>
/// <param name="threshold">閾值,通過(guò)該參數(shù)控制調(diào)節(jié)</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轉(zhuǎn)換為BitmapImage的工具方法,以供WPF的Image控件使用圖像:
public static class SystemUtils
{ /// <summary> /// 轉(zhuǎn)換類(lèi)型: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; } } }
測(cè)試效果如下:
另外,關(guān)于圖像的HSL(色相、飽和度、明度)的調(diào)節(jié),可參考在下的另一篇博文:
http://www.cnblogs.com/guxin/p/csharp-wpf-adjust-image-hue-saturation-lightness-by-imagemagick.html
參考資料:
https://softwarebydefault.com/2013/04/20/image-contrast/
總結(jié)
以上是生活随笔為你收集整理的【C#/WPF】调节图像的对比度(Contrast)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 航母上112是什么意思?
- 下一篇: 宽字节注入原理及利用