Click here to Skip to main content
15,903,755 members
Articles / Programming Languages / C#
Article

Simple ASCII Art Generator

Rate me:
Please Sign up or sign in to vote.
4.06/5 (5 votes)
18 Jul 2007CPOL 69.6K   621   36   15
Uses less than 25 lines of code to convert a given picture to an equivalent ASCII picture
Screenshot - ASCII1.jpg

Introduction

This is a very simple program to convert a given picture into an ASCII Art of 1s & 0s. The output ASCII Image is saved in *.html format.

Using the Code

The code first reads the picture specified by the user into a bitmap object. Then it scans through each pixel of the image and converts it into its equivalent HTML tag, and saves the value into the output.html file.

The final output will look similar to the picture shown above.

C#
StreamWriter sw = File.CreateText(outputPath);
Color color;
 
//Start of HTML file
sw.Write("<html> \n <body > \n <pre style=\"font: 10px/3px monospace;\">");

Random ran = new Random();
for (int y = 0; y < bmp.Height; y=y+3)
{
    for (int x = 0; x < bmp.Width; x=x+3)
    {
        int random = ran.Next(2);
        color = bmp.GetPixel(x, y);
        //write the HTML tag corresponding to the pixel color 
        //value of random will be either 1 or 0
        sw.Write("<span style=\"color: #{0:X2}{1:X2}{2:x2};\">{3}</span>", 
            (int)color.R, (int)color.G, (int)color.B, random);
    }

    sw.WriteLine("<br>");
}

//End of HTML file
sw.Write("</body>\n</html>");

sw.Close();

Note that we are using a random number generator to produce either 1 or 0 randomly.

C#
int random = ran.Next(2);

Points of Interest

This is a very simple approach to creating color ASCII art. It will be quite simple to modify the above program to output other characters as well (rather than the 1 & 0 used here).

A sample picture created with a modified version of the above program could be found here.

Note: Sometimes, it may take several seconds for the background image to load.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Sri Lanka Sri Lanka
coding in C# C++ C

==============================================

Code in English, not Gibberish Smile | :)

Comments and Discussions

 
GeneralPossibly re-inventing the wheel...there has been a similar article written prior to yours Pin
daluu30-Dec-07 14:10
daluu30-Dec-07 14:10 
GeneralRe: Possibly re-inventing the wheel...there has been a similar article written prior to yours Pin
Sajjitha Gunawardana31-Dec-07 12:42
Sajjitha Gunawardana31-Dec-07 12:42 
GeneralEasy way to show all ascii characters Pin
Will Code for Food26-Jul-07 9:26
Will Code for Food26-Jul-07 9:26 
Generalthx all the same Pin
todd_yx25-Jul-07 23:36
todd_yx25-Jul-07 23:36 
GeneralRe: thx all the same Pin
Sajjitha Gunawardana27-Jul-07 13:47
Sajjitha Gunawardana27-Jul-07 13:47 
GeneralAdditional features Pin
Papa Beers25-Jul-07 8:22
Papa Beers25-Jul-07 8:22 
Sajjitha,
Your article was featured in the Code Project Newsletter today, and it intrigued me. I started playing with it, and before I stopped myself I had added a couple of features that I now see have been asked for.

The first thing I did was to add image resizing. I used the ImageResize object from Sacha Barber, in http://www.codeproject.com/aspnet/EasyThumbs.asp.

Next I added a text box to allow me to change the text that was used in the ASCII image. I also altered the location in which the file was saved. I used a FileInfo object to get the directory for the selected image, and saved an html file using the text that was used in the image as the file name.
So a picture of my son used his name, "Aleksandr" and saved the output as "Aleksandr.html" in the same directory as the source image.

Finally I added a Trackbar to the form that allows me to change the size of the image easily.

I would like to get the new code to you, but I am not sure how to do that. It is no longer a mere 25 lines, but it seems to have more possibilities now. I may even use it to make a birthday card for my wife!

Thanks for a neat program,
Brian
Questionplz help me on C# Pin
todd_yx25-Jul-07 0:40
todd_yx25-Jul-07 0:40 
AnswerRe: plz help me on C# Pin
Sajjitha Gunawardana25-Jul-07 7:58
Sajjitha Gunawardana25-Jul-07 7:58 
GeneralLOL - Thanks, I needed the laugh! Pin
INVALID_HANDLE_VALUE12-Jul-07 21:11
INVALID_HANDLE_VALUE12-Jul-07 21:11 
GeneralIts Cool Pin
wizkiz11-Jul-07 7:11
wizkiz11-Jul-07 7:11 
GeneralIt works good Pin
bigRahn9-Jul-07 14:44
bigRahn9-Jul-07 14:44 
GeneralRe: It works good Pin
Sajjitha Gunawardana10-Jul-07 4:55
Sajjitha Gunawardana10-Jul-07 4:55 
GeneralRe: It works good Pin
bigRahn11-Jul-07 5:39
bigRahn11-Jul-07 5:39 
GeneralRe: It works good Pin
wizkiz11-Jul-07 7:13
wizkiz11-Jul-07 7:13 
GeneralRe: It works good Pin
bigRahn12-Jul-07 3:43
bigRahn12-Jul-07 3:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.