BitmapSource bitmap = null;
int degreeBrightness = ;
int degreeContrast = ; private void SetBrightness(int degree)
{
degree = degree * / ;
WriteableBitmap wb = new WriteableBitmap(bitmap);
uint[] PixelData = new uint[wb.PixelWidth * wb.PixelHeight];
wb.CopyPixels(PixelData, * wb.PixelWidth, );
for (uint y = ; y < wb.PixelHeight; y++)
{
for (uint x = ; x < wb.PixelWidth; x++)
{
uint pixel = PixelData[y * wb.PixelWidth + x];
byte[] dd = BitConverter.GetBytes(pixel);
int B = (int)dd[] + degree;
int G = (int)dd[] + degree;
int R = (int)dd[] + degree;
if (B < ) B = ;
if (B > ) B = ;
if (G < ) G = ;
if (G > ) G = ;
if (R < ) R = ;
if (R > ) R = ;
dd[] = (byte)B;
dd[] = (byte)G;
dd[] = (byte)R;
PixelData[y * wb.PixelWidth + x] = BitConverter.ToUInt32(dd, );
}
}
wb.WritePixels(new Int32Rect(, , wb.PixelWidth, wb.PixelHeight), PixelData, * wb.PixelWidth, );
//this.image.Source = wb;
}
private void SetContrast(int degree)
{
double contrast = (100.0 + degree) / 100.0;
WriteableBitmap wb = new WriteableBitmap(bitmap);
uint[] PixelData = new uint[wb.PixelWidth * wb.PixelHeight];
wb.CopyPixels(PixelData, * wb.PixelWidth, );
for (uint y = ; y < wb.PixelHeight; y++)
{
for (uint x = ; x < wb.PixelWidth; x++)
{
uint pixel = PixelData[y * wb.PixelWidth + x];
byte[] dd = BitConverter.GetBytes(pixel);
double B = (((double)dd[] / - 0.5) * contrast + 0.5) * ;
double G = (((double)dd[] / - 0.5) * contrast + 0.5) * ;
double R = (((double)dd[] / - 0.5) * contrast + 0.5) * ;
if (B < ) B = ;
if (B > ) B = ;
if (G < ) G = ;
if (G > ) G = ;
if (R < ) R = ;
if (R > ) R = ;
dd[] = (byte)B;
dd[] = (byte)G;
dd[] = (byte)R;
PixelData[y * wb.PixelWidth + x] = BitConverter.ToUInt32(dd, );
}
}
wb.WritePixels(new Int32Rect(, , wb.PixelWidth, wb.PixelHeight), PixelData, * wb.PixelWidth, );
//this.image.Source = wb;
}
private void SetCaptureImageCurrent(int degreeContrast, int degreeBrightness)
{
if (bitmap == null)
{
return;
}
WriteableBitmap wb = new WriteableBitmap(bitmap);
uint[] PixelData = new uint[wb.PixelWidth * wb.PixelHeight];
wb.CopyPixels(PixelData, * wb.PixelWidth, ); if (degreeBrightness != )
{
degreeBrightness = degreeBrightness * / ;
for (uint y = ; y < wb.PixelHeight; y++)
{
for (uint x = ; x < wb.PixelWidth; x++)
{
uint pixel = PixelData[y * wb.PixelWidth + x];
byte[] dd = BitConverter.GetBytes(pixel);
int B = (int)dd[] + degreeBrightness;
int G = (int)dd[] + degreeBrightness;
int R = (int)dd[] + degreeBrightness;
if (B < ) B = ;
if (B > ) B = ;
if (G < ) G = ;
if (G > ) G = ;
if (R < ) R = ;
if (R > ) R = ;
dd[] = (byte)B;
dd[] = (byte)G;
dd[] = (byte)R;
PixelData[y * wb.PixelWidth + x] = BitConverter.ToUInt32(dd, );
}
}
} if (degreeContrast != )
{
double contrast = (100.0 + degreeContrast) / 100.0;
for (uint y = ; y < wb.PixelHeight; y++)
{
for (uint x = ; x < wb.PixelWidth; x++)
{
uint pixel = PixelData[y * wb.PixelWidth + x];
byte[] dd = BitConverter.GetBytes(pixel);
double B = (((double)dd[] / - 0.5) * contrast + 0.5) * ;
double G = (((double)dd[] / - 0.5) * contrast + 0.5) * ;
double R = (((double)dd[] / - 0.5) * contrast + 0.5) * ;
if (B < ) B = ;
if (B > ) B = ;
if (G < ) G = ;
if (G > ) G = ;
if (R < ) R = ;
if (R > ) R = ;
dd[] = (byte)B;
dd[] = (byte)G;
dd[] = (byte)R;
PixelData[y * wb.PixelWidth + x] = BitConverter.ToUInt32(dd, );
}
}
} wb.WritePixels(new Int32Rect(, , wb.PixelWidth, wb.PixelHeight), PixelData, * wb.PixelWidth, ); PngBitmapEncoder pE = new PngBitmapEncoder();
pE.Frames.Add(BitmapFrame.Create(wb));
using (Stream stream = File.Create(SelectedImage))
{
pE.Save(stream);
}
Messenger.Default.Send(wb, "PatientImageView");
}

C#调整图片亮度和对比度的更多相关文章

  1. Opencv学习笔记4:Opencv处理调整图片亮度和对比度

    一.理论基础 在数学中我们学过线性理论,在图像亮度和对比度调节中同样适用,看下面这个公式: 在图像像素中其中: 参数f(x)表示源图像像素. 参数g(x) 表示输出图像像素. 参数a(需要满足a> ...

  2. canvas-修改图片亮度

    canvas操作-修改图片亮度 目录 canvas操作-修改图片亮度 图片亮度的概念 下面用ps截图举一个例子: 调整图片亮度的方案 实现方案一 从RGB到HSV的转换 转换的公式 javascrip ...

  3. Unity Shader-后处理:简单的颜色调整(亮度,饱和度,对比度)

    好久没坚持写blog了,是时候开始撸一波新博文了!学习Unity有一段时间了,关于Shader的书也看了几本<Unity Shader入门精要>,<Unity 3D ShaderLa ...

  4. Python-OpenCV——亮度和对比度

    亮度与对比度 亮度调整是将图像像素的强度整体变大/变小,对比度调整指的是图像暗处变得更暗,亮出变得更亮,从而拓宽某个区域内的显示精度. OpenCV中亮度和对比度应用这个公式来计算:g(x) = αf ...

  5. BackgroundCheck – 根据图片亮度智能切换元素样式

    BackgroundCheck 是一个轻量的 JavaScript 库,能够根据元素后面的图片的亮度自动切换元素样式.例如在图片幻灯片功能中,根据图片亮度调整导航箭头的颜色,这样让图片和导航的颜色形成 ...

  6. Android 播放电影时滑动屏幕调整屏幕亮度(转)

    (转自:http://blog.csdn.net/piaozhiye/article/details/6544450) 发现有一些主流的播放器播放电影时可以通过滑动屏幕调整屏幕亮度,其实实现起来也很容 ...

  7. glsl计算sprite的亮度饱和度对比度

    //glsl计算sprite的亮度饱和度对比度 #ifdef GL_ES precision mediump float; #endif uniform sampler2D u_texture; va ...

  8. Linux中如何使用gThumb批量调整图片大小

    Linux中如何使用gThumb批量调整图片大小 导读 如果你的工作涉及到图片编辑和处理,就一定会有同时对多张图片进行批量大小调整的经历.虽然大多数图片编辑应用都能够非常容易地批量调整多张图片,但对于 ...

  9. wps 批量调整图片大小 宏

    Sub 批量调整图片大小() ' ' 批量修改图片 Macro ' 宏由 zxz 录制,时间: 2014/10/29 '批量调整图片大小,避免图片太大显示不完全 '循环图片集合 For Each iS ...

随机推荐

  1. Enum,Int,String的互相转换

    Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用Int32.编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举. 注意:枚举类型的基类 ...

  2. IIS6.0解析漏洞

    IIS6.0解析漏洞分两种 1.目录解析 以*.asp命名的文件夹里的文件都将会被当成ASP文件执行. 2.文件解析 *.asp;.jpg 像这种畸形文件名在“:”后面的直接被忽略,也就是说当成 *. ...

  3. javascript 大中小括号的区别

    小括号 JavaScript中小括号有五种语义 语义1,函数声明时参数表 function func(arg1,arg2){ // ... } 语义2,和一些语句联合使用以达到某些限定作用 // 和f ...

  4. 如何从Microsoft web platform installer取得离线安装包

    有一架visual studio 2012的开发环境A由于某种原因无法链接internet, 于是乎安装officetoolsforvisual2012就有问题了. 从微软的官网上只可以下载 offi ...

  5. upper_bound()与lower_bound()的使用

    upper_bound()与lower_bound()的使用 c++中的许多库函数可以使我们的代码量大大减少,也可使问题简单化.很早之前就接触了upper_bound()与lower_bound(), ...

  6. linux下统计目录下所有子目录的大小

    du -sh * --exclude=tar |awk '{v=substr($1,length($1),1)}v=="G"{$0="1G "$0}v==&qu ...

  7. vue-cli项目打包优化(webpack3.0)

    1.修改source-map配置:此配置能大大减少打包后文件体积. a.首先修改 /config/index.js 文件: // /config/index.js dev环境:devtool: 'ev ...

  8. Extjs TreePanel API详解

    转自:http://web.qhwins.com/CSS-JS-XML/2011091312092944999107.html config定义{ animate : Boolean, contain ...

  9. 学习Android之SimpleAdapter显示网络图片

    效果图: 此程序基本的知识点是:SimpleAdapter本身是不支持网络图片的, 假设在Map.put(a,b)中 b为一个Bitmap,程序不会报红色字体,而是在控制台输出绿色的字体,例如以下 0 ...

  10. P3623 [APIO2008]免费道路

    3624: [Apio2008]免费道路 Time Limit: 2 Sec Memory Limit: 128 MBSec Special Judge Submit: 2143 Solved: 88 ...