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 ...
随机推荐
- 关于 ExpressRoute 的虚拟网络网关
虚拟网络网关用于在 Azure 虚拟网络和本地位置之间发送网络流量. 配置 ExpressRoute 连接时,必须创建并配置虚拟网络网关和虚拟网络网关连接. 创建虚拟网络网关时,需要指定几项设置. 其 ...
- Windows 7 添加快速启动栏
1.右击任务栏空白处,选择 “工具栏” ,单击 “新建工具栏” 2.输入 以下路径: %userprofile%\AppData\Roaming\Microsoft\Internet Explorer ...
- 解决由于显卡驱动BUG导致桌面右键卡顿的问题:bat文件源码
@ ECHO OFF%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe&q ...
- EntityFramework Core2.0 多对多关系配置
在EF6.0 中,多对多关系配置时,系统会自动生成第三张表,来将两张有互相约束关系的表联系起来,但是在EF Core2.0中,我们需要手动建立第三张表,比如说有两个模型Passage.cs和Cat ...
- for/while 循环全部执行完,, 中途不会停下!!中途不会停下!!中途不会停下!!! 中途不会停下!!!!!
对于这样一个在for 里面的, 他会从开始到最后走走一遍,然后再回到开始, 而不是在里面有两个操作,全部操作完第一个再往下走, 这个和我的学习方法似乎一样,真的要全局来一遍,再从新来一遍回顾问题 下面 ...
- codeforces 1007B Pave the Parallelepiped
codeforces 1007B Pave the Parallelepiped 题意 题解 代码 #include<bits/stdc++.h> using namespace std; ...
- 【原创】python web应用性能测试出现的问题以及使用的工具
uwsgi参数设置问题 问题汇总 listen 参数设置过小导致并发上不去 解决办法 更改listen参数, 请先确认linux系统的上限(tcp_max_syn_backlog,somaxconn) ...
- 学习HTTP
http://blog.csdn.net/lmh12506/article/details/7794512 HTTP协议是无状态的和Connection: keep-alive的区别 http协议是无 ...
- 智能家居中的物联网网关的可信计算平台模块(TPM)设计
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/BlueCloudMatrix/article/details/24184461 摘要: 随着智能家居 ...
- 【洛谷】【lca+结论】P3398 仓鼠找sugar
[题目描述:] 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而他的基友同时要从他的卧室 ...