如何使用C#在Windows控制台应用程序创建ASCII动画? [英] How to create ASCII animation in Windows Console application using C#?

查看:212
本文介绍了如何使用C#在Windows控制台应用程序创建ASCII动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想它显示非flickery动画像这样真棒Linux命令; SL

I would like it to display non-flickery animation like this awesome Linux command; sl

http://www.youtube.com/watch?v=9GyMZKWjcYU

我想AP preciate小&安培;说...苍蝇愚蠢的例子。

I would appreciate a small & stupid example of say ... a fly.

谢谢!

推荐答案

只需使用 Console.SetCursorPosition 的光标移动到某个位置,然后 Console.Write 字符。每一帧之前,你必须用空格覆盖它删除previous之一。继承人小例子我刚建:

Just use Console.SetCursorPosition for moving the cursor to a certain position, then Console.Write a character. Before each frame you have to delete the previous one by overwriting it with spaces. Heres a little example I just built:

class Program
{
    static void Main(string[] args)
    {
        char[] chars = new char[] { '.', '-', '+', '^', '°', '*' };
        for (int i = 0; ; i++)
        {
            if (i != 0)
            {
                // Delete the previous char by setting it to a space
                Console.SetCursorPosition(6 - (i-1) % 6 - 1, Console.CursorTop);
                Console.Write(" ");
            }

            // Write the new char
            Console.SetCursorPosition(6 - i % 6 - 1, Console.CursorTop);
            Console.Write(chars[i % 6]);

            System.Threading.Thread.Sleep(100);
        }
    }
}

您可以例如采取gif动画,从中提取出所有单个帧/图像(见这里怎么做到这一点的 ),申请一个ASCII变换(如何做到这一点说明<一href=\"http://www.c-sharpcorner.com/UploadFile/dheenu27/ImageToASCIIconverter03022007164455PM/ImageToASCIIconverter.aspx\">here例如),并打印一帧帧这些像上面code的例子。

You could for instance take an animated gif, extract all single frames/images from it (see how to do that here), apply an ASCII transformation (how to do that is described here for example) and print these frame by frame like in the above code example.

更新

只是为了好玩,我实现了我刚才所描述的。你不妨试试将 @:与路径C:\\ some_animated_gif.gif来一些(不是大)gif动画。例如采取AJAX装载机GIF从这里

Just for fun, I implemented what I just described. Just try it out replacing @"C:\some_animated_gif.gif" with the path to some (not to large) animated gif. For example take an AJAX loader gif from here.

class Program
{
    static void Main(string[] args)
    {
        Image image = Image.FromFile(@"C:\some_animated_gif.gif");
        FrameDimension dimension = new FrameDimension(
                           image.FrameDimensionsList[0]);
        int frameCount = image.GetFrameCount(dimension);
        StringBuilder sb;

        // Remember cursor position
        int left = Console.WindowLeft, top = Console.WindowTop;

        char[] chars = { '#', '#', '@', '%', '=', '+', 
                         '*', ':', '-', '.', ' ' };
        for (int i = 0; ; i = (i + 1) % frameCount)
        {
            sb = new StringBuilder();
            image.SelectActiveFrame(dimension, i);

            for (int h = 0; h < image.Height; h++)
            {
                for (int w = 0; w < image.Width; w++)
                {
                    Color cl = ((Bitmap)image).GetPixel(w, h);
                    int gray = (cl.R + cl.G + cl.B) / 3;
                    int index = (gray * (chars.Length - 1)) / 255;

                    sb.Append(chars[index]);
                }
                sb.Append('\n');
            }

            Console.SetCursorPosition(left, top);
            Console.Write(sb.ToString());

            System.Threading.Thread.Sleep(100);
        }
    }
}

这篇关于如何使用C#在Windows控制台应用程序创建ASCII动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆