Click here to Skip to main content
13,831,873 members
Click here to Skip to main content
Add your own
alternative version


186 bookmarked
Posted 26 Dec 2004
Licenced CPOL

ASCII art with C#

Rate this:
Please Sign up or sign in to vote.
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.

You may also be interested in...

Comments and Discussions

GeneralRe: Nice and can be perfect Pin
Daniel Fisher (lennybacon)26-Jan-05 2:15
memberDaniel Fisher (lennybacon)26-Jan-05 2:15 
Generaladd in install Pin
Pyro Joe22-Jan-05 17:49
memberPyro Joe22-Jan-05 17:49 
GeneralRe: add in install Pin
Daniel Fisher (lennybacon)23-Jan-05 0:23
memberDaniel Fisher (lennybacon)23-Jan-05 0:23 
GeneralA little typo in your code Pin
Eirik18-Jan-05 21:46
memberEirik18-Jan-05 21:46 
GeneralRe: A little typo in your code Pin
Daniel Fisher (lennybacon)18-Jan-05 23:15
memberDaniel Fisher (lennybacon)18-Jan-05 23:15 
Questionhow about html conversion Pin
Member 357484512-Jan-05 23:53
memberMember 357484512-Jan-05 23:53 
AnswerRe: how about html conversion Pin
Matthew Hanna26-Jun-05 9:09
memberMatthew Hanna26-Jun-05 9:09 
GeneralRe: how about html conversion Pin
Matthew Hanna26-Jun-05 14:50
memberMatthew Hanna26-Jun-05 14:50 
[Color].toHtml should be ColorTranslator.toHtml

Where are we going? And why am I in a hand basket?
Questionhow about html conversion Pin
nokiko12-Jan-05 23:53
membernokiko12-Jan-05 23:53 
GeneralVery cool btw: I ported it to Pin
5-Jan-05 21:45
suss5-Jan-05 21:45 
GeneralRe: Very cool btw: I ported it to Pin
Daniel Fisher (lennybacon)6-Jan-05 1:30
memberDaniel Fisher (lennybacon)6-Jan-05 1:30 
GeneralRe: Very cool btw: I ported it to Pin
Paul Farry6-Jan-05 9:17
professionalPaul Farry6-Jan-05 9:17 
GeneralRe: Very cool btw: I ported it to Pin
Anonymous20-Jan-05 14:31
memberAnonymous20-Jan-05 14:31 
GeneralRe: Very cool btw: I ported it to Pin
Daniel Fisher (lennybacon)21-Jan-05 1:08
memberDaniel Fisher (lennybacon)21-Jan-05 1:08 
GeneralRe: Very cool btw: I ported it to Pin
Daniel Fisher (lennybacon)14-May-07 23:48
memberDaniel Fisher (lennybacon)14-May-07 23:48 
GeneralFun Pin
Mike Ellison5-Jan-05 8:45
memberMike Ellison5-Jan-05 8:45 
GeneralOk, this is just cool.... Pin
webguy5531-Dec-04 12:29
memberwebguy5531-Dec-04 12:29 
GeneralRe: Ok, this is just cool.... Pin
Daniel Fisher (lennybacon)2-Jan-05 22:53
memberDaniel Fisher (lennybacon)2-Jan-05 22:53 
GeneralIf you like theese Pin
jumacabo29-Dec-04 5:15
memberjumacabo29-Dec-04 5:15 
GeneralYou were MSDN-blogged :-) Pin
Uwe Keim28-Dec-04 18:45
sitebuilderUwe Keim28-Dec-04 18:45 
GeneralRe: You were MSDN-blogged :-) Pin
Daniel Fisher (lennybacon)29-Dec-04 7:29
memberDaniel Fisher (lennybacon)29-Dec-04 7:29 
Questionhow to install it? Pin
Prakash Nadar27-Dec-04 22:48
memberPrakash Nadar27-Dec-04 22:48 
AnswerRe: how to install it? Pin
Daniel Fisher (lennybacon)28-Dec-04 1:37
memberDaniel Fisher (lennybacon)28-Dec-04 1:37 
GeneralWhy not include the project file? Pin
Steven Fowler28-Dec-04 4:36
memberSteven Fowler28-Dec-04 4:36 
GeneralRe: Why not include the project file? Pin
Daniel Fisher (lennybacon)28-Dec-04 15:49
memberDaniel Fisher (lennybacon)28-Dec-04 15: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190114.1 | Last Updated 31 Jan 2005
Article Copyright 2004 by Daniel Fisher (lennybacon)
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid