Click here to Skip to main content
11,639,765 members (59,769 online)
Click here to Skip to main content

ASCII art with C#

, 30 Jan 2005 355.1K 7K 178
Rate this:
Please Sign up or sign in to vote.
About writing an image to ASCII converter.

Sample Image - ascii_art_with_c_.jpg

Introduction

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" ||
    File1.PostedFile.ContentType=="image/bmp")
   {
        Output.Text = "<xmp>" +
        StaticDust.AsciiArt.ConvertImage(
         File1.PostedFile.InputStream) +
        "</xmp>";
   }
   else
   {
        ...

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));
_img.Dispose();

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();
_attributes.SetColorMatrix(_matrix);

Graphics gphGrey = Graphics.FromImage(_image);
gphGrey.DrawImage(_image, 
 bounds, 
 0, 
 0, 
 _image.Width, 
 _image.Height,
 GraphicsUnit.Pixel, 
 _attributes);

gphGrey.Dispose();

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;
        try
        {
            Color _c = _image.GetPixel(_cX, _cY);
            int _b = (int)(_c.GetBrightness() * 10);
            _allBrightness = (_allBrightness + _b);
        }
        catch
        {
            _allBrightness = (_allBrightness + 10);
        }
        ...

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

int _sb = (_allBrightness/10);
if(_sb<25)
{
    _asciiart.Append("#");
}
else if(_sb<30)
{
    ...

That's all

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

License

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

Share

About the Author


You may also be interested in...

Comments and Discussions

 
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 
I'm new here... I wonder, could you explain to me what kind of me
what kind of methods that you use to convert picture to ascii art...
I mean, for every step that you use... for grayscaling image, is that include
image compressing (what methods?) or that's vector quantitasing methods?

I am waiting for your answer.
Thank you so much.
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
kawzaki25-Jan-05 17:32
memberkawzaki25-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 
GeneralRe: add in install Pin
Daniel Fisher (lennybacon)22-Jan-05 23:23
memberDaniel Fisher (lennybacon)22-Jan-05 23:23 
GeneralA little typo in your code Pin
Eirik18-Jan-05 20:46
memberEirik18-Jan-05 20:46 
GeneralRe: A little typo in your code Pin
Daniel Fisher (lennybacon)18-Jan-05 22:15
memberDaniel Fisher (lennybacon)18-Jan-05 22:15 
Questionhow about html conversion Pin
nokiko12-Jan-05 22:53
sussnokiko12-Jan-05 22:53 
AnswerRe: how about html conversion Pin
Matthew Hanna26-Jun-05 8:09
sussMatthew Hanna26-Jun-05 8:09 
GeneralRe: how about html conversion Pin
Matthew Hanna26-Jun-05 13:50
memberMatthew Hanna26-Jun-05 13:50 
Questionhow about html conversion Pin
nokiko12-Jan-05 22:53
membernokiko12-Jan-05 22:53 
GeneralVery cool btw: I ported it to VB.net Pin
Paul Farry5-Jan-05 20:45
memberPaul Farry5-Jan-05 20:45 
GeneralRe: Very cool btw: I ported it to VB.net Pin
Daniel Fisher (lennybacon)6-Jan-05 0:30
memberDaniel Fisher (lennybacon)6-Jan-05 0:30 
GeneralRe: Very cool btw: I ported it to VB.net Pin
Paul Farry6-Jan-05 8:17
sussPaul Farry6-Jan-05 8:17 
GeneralRe: Very cool btw: I ported it to VB.net Pin
Anonymous20-Jan-05 13:31
sussAnonymous20-Jan-05 13:31 
GeneralRe: Very cool btw: I ported it to VB.net Pin
Daniel Fisher (lennybacon)21-Jan-05 0:08
memberDaniel Fisher (lennybacon)21-Jan-05 0:08 
GeneralRe: Very cool btw: I ported it to VB.net Pin
lennybacon14-May-07 22:48
memberlennybacon14-May-07 22:48 
GeneralFun Pin
Mike Ellison5-Jan-05 7:45
memberMike Ellison5-Jan-05 7:45 
GeneralOk, this is just cool.... Pin
webguy5531-Dec-04 11:29
memberwebguy5531-Dec-04 11:29 
GeneralRe: Ok, this is just cool.... Pin
Daniel Fisher (lennybacon)2-Jan-05 21:53
memberDaniel Fisher (lennybacon)2-Jan-05 21:53 
GeneralIf you like theese Pin
JuMaCaBo29-Dec-04 4:15
memberJuMaCaBo29-Dec-04 4:15 
GeneralYou were MSDN-blogged :-) Pin
Uwe Keim28-Dec-04 17:45
sitebuilderUwe Keim28-Dec-04 17:45 
GeneralRe: You were MSDN-blogged :-) Pin
Daniel Fisher (lennybacon)29-Dec-04 6:29
memberDaniel Fisher (lennybacon)29-Dec-04 6:29 
Questionhow to install it? Pin
Mr.Prakash27-Dec-04 21:48
memberMr.Prakash27-Dec-04 21:48 
AnswerRe: how to install it? Pin
Daniel Fisher (lennybacon)28-Dec-04 0:37
memberDaniel Fisher (lennybacon)28-Dec-04 0:37 
GeneralWhy not include the project file? Pin
Steven Fowler28-Dec-04 3:36
memberSteven Fowler28-Dec-04 3:36 
GeneralRe: Why not include the project file? Pin
Daniel Fisher (lennybacon)28-Dec-04 14:49
memberDaniel Fisher (lennybacon)28-Dec-04 14:49 
GeneralRe: Why not include the project file? Pin
Steven Fowler29-Dec-04 5:40
sussSteven Fowler29-Dec-04 5:40 
GeneralRe: Why not include the project file? Pin
Daniel Fisher (lennybacon)29-Dec-04 6:35
memberDaniel Fisher (lennybacon)29-Dec-04 6:35 
GeneralCool one!!! Pin
Rajesh Pillai27-Dec-04 4:33
memberRajesh Pillai27-Dec-04 4:33 
GeneralRe: Cool one!!! Pin
jhidey27-Dec-04 4:39
memberjhidey27-Dec-04 4:39 
GeneralRe: Cool one!!! Pin
Daniel Fisher (lennybacon)27-Dec-04 13:41
memberDaniel Fisher (lennybacon)27-Dec-04 13:41 

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
Web04 | 2.8.150731.1 | Last Updated 31 Jan 2005
Article Copyright 2004 by Daniel Fisher (lennybacon)
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid