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

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

资料参考:


界面滑动条两端的值是-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. div 模糊效果

    -webkit-filter:blur(3px); -moz-filter:blur(3px); filter:url(blur.svg#blur); filter: progid:DXImageTr ...

  2. HDUOJ-----1085Holding Bin-Laden Captive!

    Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  3. Windows下面安装和配置Solr 4.9(一)

       1.Solr下载 下载地址 :http://lucene.apache.org/solr/   2.解压,测试 在example文件夹中找到start.jar文件,用命令提示符运行这个文件:ja ...

  4. GDI+绘制简单图形

    #include <windows.h>#include <gdiplus.h>using namespace Gdiplus;#pragma comment(lib, &qu ...

  5. springboot http2

    转 Spring Boot With Http2 原文链接 http://www.jianshu.com/p/3d7ac535d6a0 拓展链接 http://www.jianshu.com/p/97 ...

  6. OpenGL cullface

    opengl cullface是根据顶点顺逆时针来判断正反面的.而不是根据法线判断的.所以有可能法线是正确的,但cullface效果却是反的.

  7. 修改easyui的easyloader的默认css目录路径

    easyloader默认情况下会使用js文件所在目录下的themes文件夹中的css,这里改成项目自定义的css文件夹. 首先找到: var m=src.match(/easyloader\.js(\ ...

  8. activiti自己定义流程之Spring整合activiti-modeler实例(一):环境搭建

    项目中须要整合activiti-modeler自己定义流程,找了非常多资料后,最终成功的跳转到activiti-modeler流程设计界面.下面是记录: 一.整合基础:eclipse4.4.1.tom ...

  9. iOS学习笔记37-时间和日期计算

    一.时间和日期计算 我们在应用开发中,时常须要和时间打交道,比方获取当前时间,获取两个时间点相隔的时间等等,在iOS开发中与时间相关的类有例如以下几个: 1. NSDate:表示一个绝对的时间点 2. ...

  10. JS动态创建Table,Tr,Td并赋值

    JS动态创建Table,Tr,Td并赋值. 成果库修改: 要求主题列表随成果类型改变而改变 网上查询资料后开工,在成果类型下拉框添加change()事件触发Dwr,查询主题集合——动态创建/编辑Tab ...