[译文]ASCII art with C#

 

ASCII art with C#<?xml:namespace prefix = o />

    翻译说明:原文版权归作者所有

    原文作者:Daniel Fisher (lennybacon)

    原文地址:http://www.codeproject.com/aspnet/ascii_art_with_c_.asp

           :zitiger http://zitiger.cnblogs.com/

[by Paul Farry]

<?xml:namespace prefix = v />

ascii_art_with_c_.jpg
Introduction (
介绍)

Behind the scenes: I like C# and I like ASCII art. So I asked myself if somebody has written some image to ASCII application in C#. I Googled but found nothing. I did image manipulation stuff for my company and so I decided to build a basic image-to-ASCII conversion library in C#.

背景:我喜欢C#ASCII 艺术,所以试图知道是不是已经有人使用C#写了把图像转换成ASCII的应用.Google了一下,没有发现.在公司我从事图像处理素材,所以我决定写一个把图像转换成ASCII的基本类库.

You can see a running sample here.

你能在这里看到一个运行着的例子.(译者注:我没有能够打开这个页面.)

Step 1: The Upload Form. (第一步 上传表单)

To keep it quite simple, I just check the content type and let my library do the rest.

简单起见,我只检查了上传文件的类型,其他的就交给我的类库去完成了.

if (File1.PostedFile.ContentType == " image/gif "   ||
    File1.PostedFile.ContentType
== " image/jpg "   ||
    File1.PostedFile.ContentType
== " image/jpeg "   ||
    File1.PostedFile.ContentType
== " image/pjpeg "   ||
    File1.PostedFile.ContentType
== " image/bmp " )
   {
        Output.Text 
=   " <xmp> "   +
        StaticDust.AsciiArt.ConvertImage(
         File1.PostedFile.InputStream) 
+
        
" </xmp> " ;
   }
   
else
   {
        dot.gif



Step 2: The Library(第二步 类库)

The first thing I do is of course load the image:

首先,当然是加载图片.

None.gif Image _img  =  Image.FromStream(stream);
None.gif
None.gifBitmap _image 
=  
None.gif
None.gif 
new  Bitmap(_img,  new  Size(_img.Width, _img.Height));
None.gif
None.gif_img.Dispose();
None.gif
None.gif

Next I grayscale the image - you'll see later why.

接着我把图片灰度化-过会你就知道为什么要这么做了. 

None.gif Rectangle bounds  =  
None.gif
None.gif 
new  Rectangle( 0 0 , _image.Width, _image.Height);
None.gif
None.gif 
None.gif
None.gifColorMatrix _matrix 
=   new  ColorMatrix();
None.gif
None.gif_matrix[
0 , 0 =   1 / 3f;
None.gif
None.gif_matrix[
0 , 1 =   1 / 3f;
None.gif
None.gif_matrix[
0 , 2 =   1 / 3f;
None.gif
None.gif_matrix[
1 , 0 =   1 / 3f;
None.gif
None.gif_matrix[
1 , 1 =   1 / 3f;
None.gif
None.gif_matrix[
1 , 2 =   1 / 3f;
None.gif
None.gif_matrix[
2 , 0 =   1 / 3f;
None.gif
None.gif_matrix[
2 , 1 =   1 / 3f;
None.gif
None.gif_matrix[
2 , 2 =   1 / 3f;
None.gif
None.gif 
None.gif
None.gifImageAttributes _attributes 
=  
None.gif
None.gif 
new  ImageAttributes();
None.gif
None.gif_attributes.SetColorMatrix(_matrix);
None.gif
None.gif 
None.gif
None.gifGraphics gphGrey 
=  Graphics.FromImage(_image);
None.gif
None.gifgphGrey.DrawImage(_image, 
None.gif
None.gif bounds, 
None.gif
None.gif 
0
None.gif
None.gif 
0
None.gif
None.gif _image.Width, 
None.gif
None.gif _image.Height,
None.gif
None.gif GraphicsUnit.Pixel, 
None.gif
None.gif _attributes);
None.gif
None.gif 
None.gif
None.gifgphGrey.Dispose();
None.gif
None.gif

O.K. Now, we get to the interesting part.

好了,现在我们就要进入最有意思的部分了.



for ( int  h = 0 ; h < _image.Height / 10 ; h ++ )
{
    
int  _startY  =  (h * 10 );

    
for ( int  w = 0 ; w < _image.Width / 5 ; w ++ )
    {
         
int  _startX  =  (w * 5 );
         
int  _allBrightness  =   0 ;
         dot.gif


I loop through the image's pixels and because I don't want one ASCII character per pixel, I take one per 10/5. To let every pixel influence the resulting ASCII char, I loop them and calculate the brightness of the amount.

我对图片的像素进行循环,我不想一个ASCII字符代表一个像素,而是代表10/5(像素).为了让每个

像素效果都能在最终结果的ASCII字符里有所体现,我对代表一个ASCII字符的一组像素进行循环,然后计算它们的亮度和.

(译者:把图片上的所有像素按一定的大小分成N小块,再求出每一小块内所有像素的亮度和.)

for ( int  y = 0 ; y < 10 ; y ++ )

{

    
for ( int  x = 0 ; x < 10 ; x ++ )

    {

        
int  _cY  =  y  +  _startY;

        
int  _cX  =  x  +  _startX;

        
try

        {

            Color _c 
=  _image.GetPixel(_cX, _cY);

            
int  _b  =  ( int )(_c.GetBrightness()  *   10 );

            _allBrightness 
=  (_allBrightness  +  _b);

        }

        
catch

        {

            _allBrightness 
=  (_allBrightness  +   10 );

        }

        dot.gif

Finally, I append different ASCII characters based on the calculated amount:

最后,我根据计算出来的和把不同的ASCII字符组合起来.

int  _sb  =  (_allBrightness / 10 );
if (_sb < 25 )
{
    _asciiart.Append(
" # " );
}
else   if (_sb < 30 )
{
    dot.gif



That's all

Thanks to The Code Project and Chris Maunder, newtelligence and greetings to all C# coders out there.

感谢Code Project , Chris Maunder newtelligence,并向所有C#爱好者问好.

 

译者的理解:

1.        通过Web上传一张图片;

2.        把图片进行灰度化(变成黑白);

3.        把图片划分成N个小块,对每个小块的内像素的亮度进行求和;

4.         根据每个小块的亮度平均值来选择合适的ASCII字符进行组合;

5.         输出.

 

 

欢迎杭州的朋友加入杭州.net俱乐部http://zitiger.cnblogs.com/archive/2005/07/28/201584.html.

:马上大四了,找个实习单位,个人简历http://zitiger.cnblogs.com/archive/2005/07/26/200648.html,限杭州.

转载于:https://www.cnblogs.com/zitiger/archive/2005/07/29/203083.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值