Saturday, January 12, 2013

C# method for printing ASCI images to console

Im currently taking a course in C# and the introductory is mostly console apps.
Console app are a nice, to-the-point programs, but after spending hours piecing it together its mind-rapingly terrible to watch such a dull interface.
So to spice things up abit and make the app truly your own it's nice to implement some ASCI art.


So you go download some ASCI image, for example here:
http://www.chris.com/ascii/index.php?art=cartoons/felix%20the%20cat
You copy paste it to a text file, lets call it felix.txt
Now problem is: how do you implement felix.txt into your console app?

So here is a C# method i wrote for just that. And it also implements delay. So you can make it look like a old school terminal, typing one char at a time (set timeout to 0 to just print the image)


static void printASCI_img(string file, int timeout)
        // prints ASCI text file to console
        // pause between every char given in milliseconds
        {
            try
            {
                byte[] img1;
                img1 = File.ReadAllBytes(file);

                foreach (byte i in img1)
                {
                    Console.Write(Convert.ToChar(i));
                    System.Threading.Thread.Sleep(timeout);
                }
                Console.WriteLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

No comments:

Post a Comment