Click here to Skip to main content
11,409,516 members (61,059 online)
Click here to Skip to main content

ASCII art with C#

, 31 Jan 2005
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Comments and Discussions

GeneralMy vote of 5 Pinmemberconscab18-Jan-13 2:31 
GeneralRe: My vote of 5 PinmemberDaniel Fisher (lennybacon)18-Jan-13 3:26 
QuestionHELP me what to do next PinmemberMember 97233272-Jan-13 8:54 
QuestionHELP PinmemberMember 97233272-Jan-13 8:53 
AnswerRe: HELP PinmemberDaniel Fisher (lennybacon)18-Jan-13 3:26 
Generalthe best PinmemberEvgeniy_9213-Oct-10 11:27 
GeneralYour application in Window Form flavor! PinmemberE! Ray K10-Sep-08 11:24 
GeneralAnother approach... PinmemberDoodie Woddy Doodlechips16-Mar-08 4:51 
QuestionHow to convert from Ascii to Image [modified] Pinmembernaveensri13-Jul-06 3:12 
GeneralGrayscale, GetPixel method confusion Pinmemberallapg21-Jun-05 18:08 
GeneralRe: Grayscale, GetPixel method confusion PinmemberDaniel Fisher (lennybacon)22-Jun-05 0:23 
GeneralRe: Grayscale, GetPixel method confusion PinmemberDaniel Fisher (lennybacon)22-Jun-05 0:24 
GeneralRe: Grayscale, GetPixel method confusion Pinmemberallapg22-Jun-05 19:02 
GeneralRe: Grayscale, GetPixel method confusion PinmemberDaniel Fisher (lennybacon)23-Jun-05 3:39 
GeneralCrop Image Pinmemberaaccoobb31-Mar-05 1:15 
GeneralRe: Crop Image PinmemberDaniel Fisher (lennybacon)22-Jun-05 0:25 
QuestionWhat kind of methods? PinmemberSnowjade2-Mar-05 17:03 
AnswerRe: What kind of methods? PinmemberDaniel Fisher (lennybacon)3-Mar-05 22:34 
GeneralAn improvement... PinmemberMiguel Lopes14-Feb-05 1:26 
GeneralNow this is just too cool! PinmemberGISnet29-Jan-05 20:19 
GeneralBeautiful program PinmemberPhan Dung27-Jan-05 16:25 
GeneralNice and can be perfect Pinmemberkawzaki25-Jan-05 18:32 
GeneralRe: Nice and can be perfect PinmemberDaniel Fisher (lennybacon)26-Jan-05 2:15 
Generaladd in install PinmemberPyro Joe22-Jan-05 17:49 
GeneralRe: add in install PinmemberDaniel Fisher (lennybacon)23-Jan-05 0:23 
GeneralA little typo in your code PinmemberEirik18-Jan-05 21:46 
GeneralRe: A little typo in your code PinmemberDaniel Fisher (lennybacon)18-Jan-05 23:15 
Questionhow about html conversion Pinsussnokiko12-Jan-05 23:53 
AnswerRe: how about html conversion PinsussMatthew Hanna26-Jun-05 9:09 
GeneralRe: how about html conversion PinmemberMatthew Hanna26-Jun-05 14:50 
Questionhow about html conversion Pinmembernokiko12-Jan-05 23:53 
GeneralVery cool btw: I ported it to PinmemberPaul Farry5-Jan-05 21:45 
I've just taken you lib you made, and ported it to

Interesting question: what is the performance like in C#, because in the last 2 lines of the VB version, there is a bit of a slow down in the generation.
GeneralRe: Very cool btw: I ported it to PinmemberDaniel Fisher (lennybacon)6-Jan-05 1:30 
GeneralRe: Very cool btw: I ported it to PinsussPaul Farry6-Jan-05 9:17 
GeneralRe: Very cool btw: I ported it to PinsussAnonymous20-Jan-05 14:31 
GeneralRe: Very cool btw: I ported it to PinmemberDaniel Fisher (lennybacon)21-Jan-05 1:08 
GeneralRe: Very cool btw: I ported it to Pinmemberlennybacon14-May-07 23:48 
GeneralFun PinmemberMike Ellison5-Jan-05 8:45 
GeneralOk, this is just cool.... Pinmemberwebguy5531-Dec-04 12:29 
GeneralRe: Ok, this is just cool.... PinmemberDaniel Fisher (lennybacon)2-Jan-05 22:53 
GeneralIf you like theese PinmemberJuMaCaBo29-Dec-04 5:15 
GeneralYou were MSDN-blogged :-) PinsitebuilderUwe Keim28-Dec-04 18:45 
GeneralRe: You were MSDN-blogged :-) PinmemberDaniel Fisher (lennybacon)29-Dec-04 7:29 
Questionhow to install it? PinmemberMr.Prakash27-Dec-04 22:48 
AnswerRe: how to install it? PinmemberDaniel Fisher (lennybacon)28-Dec-04 1:37 
GeneralWhy not include the project file? PinmemberSteven Fowler28-Dec-04 4:36 
GeneralRe: Why not include the project file? PinmemberDaniel Fisher (lennybacon)28-Dec-04 15:49 
GeneralRe: Why not include the project file? PinsussSteven Fowler29-Dec-04 6:40 
GeneralRe: Why not include the project file? PinmemberDaniel Fisher (lennybacon)29-Dec-04 7:35 
GeneralCool one!!! PinmemberRajesh Pillai27-Dec-04 5:33 

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

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

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