关于对比度:
调节对比度直观感受是,高对比度的图像明暗关系更明显,色彩更鲜艳;低对比度的图像表面像是蒙上一层灰,色彩不鲜艳。

需求:
制作一个面板,一个滑动条,拖动滑动条可以修改目标图片的对比度。

资料参考:


界面滑动条两端的值是-30~30,默认处于中间位置0。已知目标图像的Bitmap数据。

  1. 修改Bitmap的对比度。
  2. 将修改之后的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(, ,
sourceBitmap.Width, sourceBitmap.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); byte[] pixelBuffer = new byte[sourceData.Stride * sourceData.Height]; Marshal.Copy(sourceData.Scan0, pixelBuffer, , pixelBuffer.Length); sourceBitmap.UnlockBits(sourceData); double contrastLevel = Math.Pow((100.0 + threshold) / 100.0, ); double blue = ;
double green = ;
double red = ; for (int k = ; k + < pixelBuffer.Length; k += )
{
blue = ((((pixelBuffer[k] / 255.0) - 0.5) *
contrastLevel) + 0.5) * 255.0; green = ((((pixelBuffer[k + ] / 255.0) - 0.5) *
contrastLevel) + 0.5) * 255.0; red = ((((pixelBuffer[k + ] / 255.0) - 0.5) *
contrastLevel) + 0.5) * 255.0; if (blue > )
{ blue = ; }
else if (blue < )
{ blue = ; } if (green > )
{ green = ; }
else if (green < )
{ green = ; } if (red > )
{ red = ; }
else if (red < )
{ red = ; } pixelBuffer[k] = (byte)blue;
pixelBuffer[k + ] = (byte)green;
pixelBuffer[k + ] = (byte)red;
} Bitmap resultBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height); BitmapData resultData = resultBitmap.LockBits(new Rectangle(, ,
resultBitmap.Width, resultBitmap.Height),
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); Marshal.Copy(pixelBuffer, , 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 = ;
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(色相、饱和度、明度)的调节,可参考在下的另一篇博文:


参考资料:

https://softwarebydefault.com/2013/04/20/image-contrast/

【C#/WPF】调节图像的对比度(Contrast)的更多相关文章

  1. 【C#/WPF】调节图像的HSL(色相Hue、饱和度Saturation、明亮度Lightness)

    先说概念: HSL是一种描述颜色的方式,其他颜色描述方式还有大家熟悉的RGB值.HSL三个字母分别表示图像的Hue色相.Saturation饱和度.Lightness明亮度. 需求: 制作一个面板,包 ...

  2. 【C#/WPF】调节图像的HSL(色相、饱和度、明亮度)

    原文:[C#/WPF]调节图像的HSL(色相.饱和度.明亮度) 先说概念: HSL是一种描述颜色的方式(其他颜色描述方式还有大家熟悉的RGB值).HSL三个字母分别表示图像的Hue色相.Saturat ...

  3. OpenCV学习:改变图像的对比度和亮度

    本实例演示简单地改变图像的对比度和亮度,使用了如下线性变换来实现像素值的遍历操作: The parameters α > 0 and β often called the gain and bi ...

  4. Python: PS 图像调整--对比度调整

    本文用 Python 实现 PS 里的图像调整–对比度调整.具体的算法原理如下: (1).nRGB = RGB + (RGB - Threshold) * Contrast / 255 公式中,nRG ...

  5. OpenCV --- 修改图像的对比度、亮度 、RGB转Gray图像、修改图像的尺寸

    #include <opencv2/core.hpp> #include <opencv2/imgcodecs.hpp> #include <opencv2/highgu ...

  6. 借助Photoshop,Illustrator等设计软件进行WPF图形图像的绘制

    原文:借助Photoshop,Illustrator等设计软件进行WPF图形图像的绘制 本文所示例子是借助第三方设计软件,制作复杂的矢量图形,转成与XAML酷似的SVG,再转换成xaml而实现的. 这 ...

  7. theano 实现图像局部对比度归一化

    很多时候我们需要对图像进行局部对比度归一化,比如分块CNN的预处理阶段.theano对此提供了一些比较方便的操作. 局部归一化的一种简单形式为: 其中μ和σ分别为局部(例如3x3的小块)的均值和标准差 ...

  8. WPF图形图像相关类

    BitmapMetadata类: 继承自抽象类ImageMetadata,包含图像的原数据信息,如相机型号.图像修改程序名称.拍照日期.拍照地点等.ImageSoure类包含ImageMetadata ...

  9. OpenCV 改变图像的对比度和亮度

    #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <ios ...

随机推荐

  1. Linux(CentOS)的server安装及配置图解(图文)

    CentOS,据说这个如今在server上装机量非常大. 安装的分区划分 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdXBpMnU=/font/5a6L ...

  2. POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 Desc ...

  3. AWK中的OFS的问题

    echo a b c d |awk '{OFS = ":";print $0}' 我的理解是应该把输出显示为如下的方式 a:b:c:d dan但执行的结果不是这样的 a b c d ...

  4. poj----Maximum sum(poj 2479)

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30704   Accepted: 9408 Desc ...

  5. Android中可以做的两件坏事——破解锁屏密码和获取Wifi密码

    来源:http://blog.csdn.net/jiangwei0910410003/article/details/41800409 今天咋们来看一下我在现实中遇到的两个问题和解决方案 问题一:忘记 ...

  6. H5版如何在微信外(非微信浏览器)进行微信支付技术方案

    官方是支持在非微信内置浏览器中调起微信支付的!H5支付是基于公众号基础开发的一种非微信内浏览器支付方式(需要单独申请支付权限),可以满足在微信外的手机H5页面进行微信支付的需求.同时,由于H5链接传播 ...

  7. 【jQuery】清空表单内容

    function resertForm(){ $(':input','#formId') .not(':button, :submit, :reset, :hidden') .val('') .rem ...

  8. Redis客户端

    1.自带的: Redis-cli 2.redis-desktop-manager-0.7.9.809  是一个图形化客户端 但是不支持集群  **由于linux防火墙默认开启,redis的服务端口63 ...

  9. C#开发Windows Services服务--服务安装失败的解决办法

    问题1:“System.Security.SecurityException:未找到源,但未能搜索某些或全部事件日志.不可访问的日志: Security.” 正在运行事务处理安装. 正在开始安装的“安 ...

  10. Android oncreate onupgrade什么时候被调用

    在学习Android数据库SQLite之前,必须意识到这一点,目前在Android系统中集成的是SQLite3 版本,SQLite是一个开源的嵌入式数据库,他支持NULL.INTEGER.REAL.T ...