Click here to Skip to main content
14,332,426 members

ASCII art with C#

Rate this:
4.85 (78 votes)
Please Sign up or sign in to vote.
4.85 (78 votes)
30 Jan 2005CPOL
About writing an image to ASCII converter.

Sample Image - ascii_art_with_c_.jpg


Behind the scenes: I like C# and I like ASCII art. So I asked myself if somebody has written some image to ASCII application in C#. I Googled but found nothing. I did image manipulation stuff for my company and so I decided to build a basic image-to-ASCII conversion library in C#.

You can see a running sample here.

Step 1: The Upload Form.

To keep it quite simple, I just check the content type and let my library do the rest.

if(File1.PostedFile.ContentType=="image/gif" ||
    File1.PostedFile.ContentType=="image/jpg" ||
    File1.PostedFile.ContentType=="image/jpeg" ||
    File1.PostedFile.ContentType=="image/pjpeg" ||
        Output.Text = "<xmp>" +
         File1.PostedFile.InputStream) +

Step 2: The Library

The first thing I do is of course load the image:

Image _img = Image.FromStream(stream);
Bitmap _image = 
 new Bitmap(_img, new Size(_img.Width, _img.Height));

Next I grayscale the image - you'll see later why.

Rectangle bounds = 
 new Rectangle(0, 0, _image.Width, _image.Height);

ColorMatrix _matrix = new ColorMatrix();
_matrix[0,0] = 1/3f;
_matrix[0,1] = 1/3f;
_matrix[0,2] = 1/3f;
_matrix[1,0] = 1/3f;
_matrix[1,1] = 1/3f;
_matrix[1,2] = 1/3f;
_matrix[2,0] = 1/3f;
_matrix[2,1] = 1/3f;
_matrix[2,2] = 1/3f;

ImageAttributes _attributes = 
 new ImageAttributes();

Graphics gphGrey = Graphics.FromImage(_image);


O.K. Now, we get to the interesting part.

for(int h=0; h<_image.Height/10; h++)
    int _startY = (h*10);

    for(int w=0; w<_image.Width/5; w++)
         int _startX = (w*5);
         int _allBrightness = 0;

I loop through the image's pixels and because I don't want one ASCII character per pixel, I take one per 10/5. To let every pixel influence the resulting ASCII char, I loop them and calculate the brightness of the amount.

for(int y=0; y<10; y++)
    for(int x=0; x<10; x++)
        int _cY = y + _startY;
        int _cX = x + _startX;
            Color _c = _image.GetPixel(_cX, _cY);
            int _b = (int)(_c.GetBrightness() * 10);
            _allBrightness = (_allBrightness + _b);
            _allBrightness = (_allBrightness + 10);

Finally, I append different ASCII characters based on the calculated amount:

int _sb = (_allBrightness/10);
else if(_sb<30)

That's all

Thanks to The Code Project and Chris Maunder, newtelligence and greetings to all C# coders out there.


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


About the Author

Daniel Fisher (lennybacon)
Software Developer
Germany Germany
Beginning of the nineties started to assemble computers and configure networks. Automation lead to batching and scripting. Arrived on the other side of the trench the HTTP protocol is a constant companion. the journey began with JavaScript, then Perl, PHP and ASP with Visual Basic 5 and JScript, ending with Java and C++. End of the nineties starting to focus .NET, streaked Python, and now JavaScript again. He develops, teaches, trains, coaches and speaks. His topics: HTML5 & Web, Data Access & Performance, Scalable & Testable Designs, Distributed Systems & Services, Security & Trust.

Comments and Discussions

Bugbroken link to see example Pin
_Noctis_20-Dec-15 16:32
professional_Noctis_20-Dec-15 16:32 
GeneralMy vote of 5 Pin
ConstantinCabiniuc18-Jan-13 1:31
memberConstantinCabiniuc18-Jan-13 1:31 
GeneralRe: My vote of 5 Pin
Daniel Fisher (lennybacon)18-Jan-13 2:26
memberDaniel Fisher (lennybacon)18-Jan-13 2:26 
QuestionHELP me what to do next Pin
Member 97233272-Jan-13 7:54
memberMember 97233272-Jan-13 7:54 
QuestionHELP Pin
Member 97233272-Jan-13 7:53
memberMember 97233272-Jan-13 7:53 
AnswerRe: HELP Pin
Daniel Fisher (lennybacon)18-Jan-13 2:26
memberDaniel Fisher (lennybacon)18-Jan-13 2:26 
Generalthe best Pin
Evgeniy_9213-Oct-10 10:27
memberEvgeniy_9213-Oct-10 10:27 
GeneralYour application in Window Form flavor! Pin
E! Ray K10-Sep-08 10:24
memberE! Ray K10-Sep-08 10:24 
GeneralAnother approach... Pin
Doodie Woddy Doodlechips16-Mar-08 3:51
memberDoodie Woddy Doodlechips16-Mar-08 3:51 
QuestionHow to convert from Ascii to Image [modified] Pin
naveen.srikakolanu13-Jul-06 2:12
membernaveen.srikakolanu13-Jul-06 2:12 
GeneralGrayscale, GetPixel method confusion Pin
allapg21-Jun-05 17:08
memberallapg21-Jun-05 17:08 
GeneralRe: Grayscale, GetPixel method confusion Pin
Daniel Fisher (lennybacon)21-Jun-05 23:23
memberDaniel Fisher (lennybacon)21-Jun-05 23:23 
GeneralRe: Grayscale, GetPixel method confusion Pin
Daniel Fisher (lennybacon)21-Jun-05 23:24
memberDaniel Fisher (lennybacon)21-Jun-05 23:24 
GeneralRe: Grayscale, GetPixel method confusion Pin
allapg22-Jun-05 18:02
memberallapg22-Jun-05 18:02 
GeneralRe: Grayscale, GetPixel method confusion Pin
Daniel Fisher (lennybacon)23-Jun-05 2:39
memberDaniel Fisher (lennybacon)23-Jun-05 2:39 
GeneralCrop Image Pin
aaccoobb31-Mar-05 0:15
memberaaccoobb31-Mar-05 0:15 
GeneralRe: Crop Image Pin
Daniel Fisher (lennybacon)21-Jun-05 23:25
memberDaniel Fisher (lennybacon)21-Jun-05 23:25 
QuestionWhat kind of methods? Pin
Snowjade2-Mar-05 16:03
memberSnowjade2-Mar-05 16:03 
AnswerRe: What kind of methods? Pin
Daniel Fisher (lennybacon)3-Mar-05 21:34
memberDaniel Fisher (lennybacon)3-Mar-05 21:34 
GeneralAn improvement... Pin
Miguel Lopes14-Feb-05 0:26
memberMiguel Lopes14-Feb-05 0:26 
GeneralNow this is just too cool! Pin
GISnet29-Jan-05 19:19
memberGISnet29-Jan-05 19:19 
GeneralBeautiful program Pin
Phan Dung27-Jan-05 15:25
memberPhan Dung27-Jan-05 15:25 
GeneralNice and can be perfect Pin
Member 1646325-Jan-05 17:32
memberMember 1646325-Jan-05 17:32 
GeneralRe: Nice and can be perfect Pin
Daniel Fisher (lennybacon)26-Jan-05 1:15
memberDaniel Fisher (lennybacon)26-Jan-05 1:15 
Generaladd in install Pin
Pyro Joe22-Jan-05 16:49
memberPyro Joe22-Jan-05 16:49 

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.

Posted 26 Dec 2004


188 bookmarked