首 页 | 新 闻 | 技术中心 | 第二书店 | 《程序员》 | 《开发高手》 | 社 区 | 黄 页 | 人 才
移 动专 题SUNIBM微 软微 创精 华Donews人 邮
我的技术中心 
我的分类 我的文档
全部文章 发表文章
专栏管理 使用说明



 RSS 订阅 
最新文档列表
Windows/.NET
.NET  (rss)    
Visual C++  (rss)    
Delphi  (rss)    
Visual Basic  (rss)    
ASP  (rss)    
JavaScript  (rss)    
Java/Linux
Java  (rss)    
Perl  (rss)    
综合
其他开发语言  (rss)    
文件格式  (rss)    
企业开发
游戏开发  (rss)    
网站制作技术  (rss)    
数据库
数据库开发  (rss)    
软件工程
其他  (rss)    

积极原创作者 
wangchinaking (57)
yjz0065 (113)
coofucoo (105)
Drate (69)
lphpc (30)
smallnest (61)
iiprogram (64)
downmoon (32)
danny_xcz (49)
btbtd (81)
CSDN - 文档中心 - .NET 阅读:6738   评论: 0    参与评论
标题   [GDI+]如何将一个彩色图像转换成黑白图像     选择自 hbzxf 的 Blog
关键字   [GDI+]如何将一个彩色图像转换成黑白图像
出处  

彩色图像转换为黑白图像时需要计算图像中每像素有效的亮度值,通过匹配像素

亮度值可以轻松转换为黑白图像。

计算像素有效的亮度值可以使用下面的公式:

Y=0.3RED+0.59GREEN+0.11Blue

然后使用 Color.FromArgb(Y,Y,Y) 来把计算后的值转换

转换代码可以使用下面的方法来实现:

[C#]

public Bitmap ConvertToGrayscale(Bitmap source)

{

  Bitmap bm 
= new Bitmap(source.Width,source.Height);

  
for(int y=0;y<bm.Height;y++)

  
{

    
for(int x=0;x<bm.Width;x++)

    
{

      Color c
=source.GetPixel(x,y);

      
int luma = (int)(c.R*0.3 + c.G*0.59+ c.B*0.11);

      bm.SetPixel(x,y,Color.FromArgb(luma,luma,luma));

    }


  }


  
return bm;

}

 

 

[VB]

Public Function ConvertToGrayscale(ByVal source As Bitmap) as Bitmap

  
Dim bm as new Bitmap(source.Width,source.Height)

  
Dim x

  
Dim y

  
For y=0 To bm.Height

    
For x=0 To bm.Width

      
Dim c as Color = source.GetPixel(x,y)

      
Dim luma as Integer = CInt(c.R*0.3 + c.G*0.59 + c.B*0.11)

      bm.SetPixel(x,y,Color.FromArgb(luma,luma,luma)

    
Next

  
Next

  
Return bm

End Function


当然了这是一个好的方法,如果需要更简单的做到图像的色彩转换还可以使用ColorMatrix类,下一篇我们将介绍

[待续...]


相关文章
对该文的评论