使用asp.net改变图片颜色如灰色的变成彩色

所属分类: 网络编程 / ASP.NET 阅读数: 872
收藏 0 赞 0 分享

最近奇葩经理提出了奇葩的需求,要能在网站上改变图片的颜色,比如灰色的变成彩色,彩色的变成灰色,尼玛楼主的感受你们不懂!于是有了下面的代码。

用法:调用update_pixelColor方法并传参数即可

#region 改变图片颜色 

/// <summary> 
/// 改变图片的颜色 
/// </summary> 
/// <param name="filePath">图片的完整路径</param> 
/// <param name="colorIndex">改变的颜色,true为灰色,false为彩色</param> 
public void update_pixelColor(string filePath, bool colorIndex) 
{ 
Bitmap bmp = new Bitmap(Bitmap.FromFile(filePath)); 

int value = 0; 

for (int i = 0; i < bmp.Height; i++) 
{ 
for (int j = 0; j < bmp.Width; j++) 
{ 
if (colorIndex) 
value = this.GetGrayNumColor(bmp.GetPixel(j, i)); 
else 
value = this.GetHongNumColor(bmp.GetPixel(j, i)); 

bmp.SetPixel(j, i, Color.FromArgb(value, value, value)); 
} 
} 

bmp.Save(filePath); 
} 

/// <summary> 
/// 获取彩色单点像素 
/// </summary> 
/// <param name="posClr">单点像素</param> 
/// <returns>int</returns> 
private int GetHongNumColor(Color posClr) 
{ 
return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16; 
} 

/// <summary> 
/// 获取灰色单点像素 
/// </summary> 
/// <param name="posClr">单点像素</param> 
/// <returns>Color</returns> 
private int GetGrayNumColor(Color posClr) 
{ 
//要改变ARGB 
return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16; 
} 

#endregion 改变图片颜色 
更多精彩内容其他人还在看

用ASP.Net实现文件的在线压缩和解压缩

用ASP.Net实现文件的在线压缩和解压缩
收藏 0 赞 0 分享

ASP.NET中文件上传下载方法集合

ASP.NET中文件上传下载方法集合
收藏 0 赞 0 分享

ASP.NET通过Remoting service上传文件

ASP.NET通过Remoting service上传文件
收藏 0 赞 0 分享

ASP.NET2.0服务器控件之Render方法

ASP.NET2.0服务器控件之Render方法
收藏 0 赞 0 分享

ASP.NET2.0 WebRource,开发微调按钮控件

ASP.NET2.0 WebRource,开发微调按钮控件
收藏 0 赞 0 分享

ASP.NET2.0新特性概述

ASP.NET2.0新特性概述
收藏 0 赞 0 分享

介绍几个ASP.NET中容易忽略但却很重要的方法函数

介绍几个ASP.NET中容易忽略但却很重要的方法函数
收藏 0 赞 0 分享

asp.net2.0如何加密数据库联接字符串

asp.net2.0如何加密数据库联接字符串
收藏 0 赞 0 分享

用.NET 2.0压缩/解压功能处理大型数据

用.NET 2.0压缩/解压功能处理大型数据
收藏 0 赞 0 分享

ASP.NET入门随想之检票的老太太

ASP.NET入门随想之检票的老太太
收藏 0 赞 0 分享
查看更多