Click here to Skip to main content
6,629,885 members and growing! (22,909 online)
Email Password   helpLost your password?
Languages » C# » General     Beginner License: The Code Project Open License (CPOL)

Simple ASCII Art Generator

By Sajjitha Gunawardana

Uses less than 25 lines of code to convert a given picture to an equivalent ASCII picture
C# 2.0, Windows, .NET 2.0VS2005, Dev
Posted:4 Jul 2007
Updated:18 Jul 2007
Views:22,871
Bookmarked:29 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
3 votes for this article.
Popularity: 1.54 Rating: 3.22 out of 5

1
1 vote, 33.3%
2

3
1 vote, 33.3%
4
1 vote, 33.3%
5
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.

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.

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)

About the Author

Sajjitha Gunawardana


Member
coding in C# C++ C

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

Before C it seems, there was B. And before B there was Darkness ... Smile
Occupation: Web Developer
Location: Sri Lanka Sri Lanka

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
GeneralPossibly re-inventing the wheel...there has been a similar article written prior to yours Pinmemberdaluu15:10 30 Dec '07  
GeneralRe: Possibly re-inventing the wheel...there has been a similar article written prior to yours PinmemberSajjitha Gunawardana13:42 31 Dec '07  
GeneralEasy way to show all ascii characters PinmemberWill Code for Food10:26 26 Jul '07  
Generalthx all the same Pinmembertodd_yx0:36 26 Jul '07  
GeneralRe: thx all the same PinmemberSajjitha Gunawardana14:47 27 Jul '07  
GeneralAdditional features PinmemberPapa Beers9:22 25 Jul '07  
Questionplz help me on C# Pinmembertodd_yx1:40 25 Jul '07  
AnswerRe: plz help me on C# PinmemberSajjitha Gunawardana8:58 25 Jul '07  
GeneralLOL - Thanks, I needed the laugh! PinmemberINVALID_HANDLE_VALUE22:11 12 Jul '07  
GeneralIts Cool Pinmemberwizkiz8:11 11 Jul '07  
GeneralIt works good PinmemberbigRahn15:44 9 Jul '07  
GeneralRe: It works good PinmemberSajjitha Gunawardana5:55 10 Jul '07  
GeneralRe: It works good PinmemberbigRahn6:39 11 Jul '07  
GeneralRe: It works good Pinmemberwizkiz8:13 11 Jul '07  
GeneralRe: It works good PinmemberbigRahn4:43 12 Jul '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Jul 2007
Editor: Deeksha Shenoy
Copyright 2007 by Sajjitha Gunawardana
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project