首页 > 其他 > 详细

ASCII Art

时间:2018-02-18 00:56:06      阅读:292      评论:0      收藏:0      [点我收藏+]

Conmajia, 2012
Updated on Feb. 18, 2018

What is ASCII art?

It‘s graphic symbols formed by ASCII characters instead of colorful pixels. ASCII art is an old technique when the network was not that developed as today. Net bandwidth was so limited and expensive those days, that people had to figure out some substitutions for images. Here is an example to show an excellent or excited emotion icon - ヾ(≧?≦*)ゝ.

In this article, I tried to use a brightness-to-character palette to build ASCII art from an pixel-based image.

The core code is less than 50 lines as below.

public static string Generate(Bitmap img, int rowSz, int colSz)
{
    StringBuilder sb
        = new StringBuilder(
        img.Width / colSz * img.Height / rowSz
        );
    FastBitmap fast
        = new FastBitmap(
        img
        );

    fast.Lock();
    for (int h = 0; h < img.Height / rowSz; h++)
    {
        int yoffset = h * rowSz;
        for (int w = 0; w < img.Width / colSz; w++)
        {
            int xoffset = w * colSz;
            int br = 0;

            for (int y = 0; y < 10; y++)
                for (int x = 0; x < 10; x++)
                    try
                    {
                        Color c = fast.GetPixel(
                            x + xoffset,
                            y + yoffset);
                        br =
                            br + (int)(c.GetBrightness() * 10);
                    }
                    catch
                        br += 10;
            br /= 10;
            if (br / 5 < charset.Length)
                sb.Append(
                    charset[br / 5]);
            else
                sb.Append(' ');
        }
        sb.AppendLine();
    }
    fast.Unlock();

    return sb.ToString();
}

Below is a preview:

技术分享图片

You can even make this program animated like this:

技术分享图片

\(\Box\)

ASCII Art

原文:https://www.cnblogs.com/conmajia/p/ascii-art-v2.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!