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. kvm 启动libvirtd时出现错误

    kvm 启动libvirtd市出现错误: /etc/init.d/libvirtd start启动 libvirtd 守护进程:libvirtd: relocation error: libvirtd ...

  2. js拼接字符串,字符串转数组

    想要把字符串按一定的规则拼起来如 1,2,3 var a = []; a.push(1); a.push(2); a.push(3); a.join(','); =>> 1,2,3 想要把 ...

  3. linux something

    使用的UbuntuKylin  source error e1:apt-get update提示没有证书 e2:访问不了阿里云源服务器 e3:GPG 错误:http://download.mono-p ...

  4. iOS8中的定位服务

    iOS8中的定位服务 My app that worked fine in iOS 7 doesn't work with the iOS 8 SDK. CLLocationManager doesn ...

  5. PHP CLI模式下echo换行

    近日在执行导库程序的时候,需要在CLI模式下运行程序进行调试,如下图,这是什么鬼?不是我想要的结果 后经过查资料发现代码中执行的输出为 //错误方法实例 echo '其他-683\n'; //正确打开 ...

  6. springmvc常用的组件,注解,跳转

    路径映射 XXXHandlerMapping 随开发配置越来越多 注解到java代码中来简化xml配置 请求到哪个Controller 控制器bean Controller 随着开发配置越来越多 注解 ...

  7. 实践和感悟 - scala向下转型和减少穷举

    工作中的问题总结: 问题一:scala 之向下转型 引言:假如在复杂的业务逻辑中,变量的类型不能确认,只能给个接口类型,这样数据类型推导不会错误,但是后面要使用实现类的类型时,你却发现转不过来了? 对 ...

  8. 团队作业1——团队展示&教辅宝

    1.队名:PHILOSOPHER 2.队员学号: [组长]金盛昌(201421122043).刘文钊(20142112255).陈笑林(201421122042). 张俊逸(201421122044) ...

  9. prometheus-入门尝试

    prometheus-入门 Prometheus 是由 SoundCloud 开源监控告警解决方案2015 年在 github 上开源以来,已经吸引了 很多大公司的使用:2016 年 Promethe ...

  10. iPhone X进入DFU模式

    下面把iphoneX进dfu模式的方法分享给大家.在开机状态下,按一下音量加键后松开,然后按一下音量减键后松开,接着按住电源键,屏幕完全熄灭后,松开电源键,接着同时按住电源键和音量减,保持5秒左右,再 ...