C#调整图片亮度和对比度
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#调整图片亮度和对比度的更多相关文章
- Opencv学习笔记4:Opencv处理调整图片亮度和对比度
一.理论基础 在数学中我们学过线性理论,在图像亮度和对比度调节中同样适用,看下面这个公式: 在图像像素中其中: 参数f(x)表示源图像像素. 参数g(x) 表示输出图像像素. 参数a(需要满足a> ...
- canvas-修改图片亮度
canvas操作-修改图片亮度 目录 canvas操作-修改图片亮度 图片亮度的概念 下面用ps截图举一个例子: 调整图片亮度的方案 实现方案一 从RGB到HSV的转换 转换的公式 javascrip ...
- Unity Shader-后处理:简单的颜色调整(亮度,饱和度,对比度)
好久没坚持写blog了,是时候开始撸一波新博文了!学习Unity有一段时间了,关于Shader的书也看了几本<Unity Shader入门精要>,<Unity 3D ShaderLa ...
- Python-OpenCV——亮度和对比度
亮度与对比度 亮度调整是将图像像素的强度整体变大/变小,对比度调整指的是图像暗处变得更暗,亮出变得更亮,从而拓宽某个区域内的显示精度. OpenCV中亮度和对比度应用这个公式来计算:g(x) = αf ...
- BackgroundCheck – 根据图片亮度智能切换元素样式
BackgroundCheck 是一个轻量的 JavaScript 库,能够根据元素后面的图片的亮度自动切换元素样式.例如在图片幻灯片功能中,根据图片亮度调整导航箭头的颜色,这样让图片和导航的颜色形成 ...
- Android 播放电影时滑动屏幕调整屏幕亮度(转)
(转自:http://blog.csdn.net/piaozhiye/article/details/6544450) 发现有一些主流的播放器播放电影时可以通过滑动屏幕调整屏幕亮度,其实实现起来也很容 ...
- glsl计算sprite的亮度饱和度对比度
//glsl计算sprite的亮度饱和度对比度 #ifdef GL_ES precision mediump float; #endif uniform sampler2D u_texture; va ...
- Linux中如何使用gThumb批量调整图片大小
Linux中如何使用gThumb批量调整图片大小 导读 如果你的工作涉及到图片编辑和处理,就一定会有同时对多张图片进行批量大小调整的经历.虽然大多数图片编辑应用都能够非常容易地批量调整多张图片,但对于 ...
- wps 批量调整图片大小 宏
Sub 批量调整图片大小() ' ' 批量修改图片 Macro ' 宏由 zxz 录制,时间: 2014/10/29 '批量调整图片大小,避免图片太大显示不完全 '循环图片集合 For Each iS ...
随机推荐
- 从MySQL向Greenplum集群中导入数据
我们要从MySQL当中导出数据到Greenplum当中,按照以下步骤就可以 1:将MySQL当中的表导出外部文件 以schema_name.table_name为例 select product_id ...
- 《SQL Server 2008从入门到精通》--20180628
数据库基本概念:区.页.行 区:SQL Server中管理空间的基本单位.一个区大小为64KB,是八个物理上连续的页.SQL Server中每MB有16个区.一旦一个区被存储满,SQL Server将 ...
- 委托学习总结(二)匿名方法和lambda表达式
之前总结了委托这个困惑着大多初学者的概念,继续来学习匿名方法和lambda表达式 (1)我们之前写了这样一段代码 //自定义一个委托 public delegate int Expression(in ...
- 设计多选一按钮ChooseOnlyButton
设计多选一按钮ChooseOnlyButton 效果: 源码: ChooseOnlyButton.h 与 ChooseOnlyButton.m // // ChooseOnlyButton.h // ...
- 使用SDWebImage淡入淡出的方式加载图片
使用SDWebImage淡入淡出的方式加载图片 效果: 请通过以下方式下载源码: 找到它修改文件的地方: 以下是使用源码: // // ViewController.m // SDWebImageFa ...
- linux centos6.5 网络配置
1.方法一.修改网络配置文件 ①cd /etc/sysconfig/network-scripts ②cp ifcfg-eth0 ./ifcfg-eth0.bak //修改前先备份 第一个以太网 ...
- Hybris阶段总结(2)hybris架构
年前总结一下这两个星期在SAP实习学到的一些东西 先上图: 从底往上总结,之后会有个小例子来解释一下 1.Persistence layer 就是作为hybris所连接的数据库这一层,其中hybr ...
- 【C#】#101 导入导出Excel
项目需求: 1.把数据导出到Excel: 2.把Excel数据导入到数据库 使用的类库: Aspose.Cells ExcelHelper.zip下载 一.导出[调用已经封装好的方法][未 ...
- springmvc入门之HelloWorld篇
springmvc是一个基于spring的mvc框架,各种优点啥的用过就知道了.下面开始讲HelloWorldController的实现. 1.开发环境搭建<导jar包+配置文件> 1.1 ...
- 将本地已有项目上传到github
1.在github上创建一个文件 2.看本地C盘中是否有.ssh文件夹 (C:\Users\用户名\.ssh) 检测有没有.ssh文件夹:执行命令 cd ~/.ssh 如果没有的话执行git命令: ...