Click here to Skip to main content
Click here to Skip to main content

ASCII art with C#

By , 30 Jan 2005
 

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

About the Author

Daniel Fisher (lennybacon)
Web Developer
Germany Germany
Member
Daniel Fisher is co-founder of devcoach. He supports since 1997 customers and projects in Germany and throughout Europe. As a consultant and developer he has worked several years on projects for insurance companies, distributors, mobile communications hardware, construction supplies and various other companies of different branches. He has a strong emphasis on service-orientation, agile methods, the web and data access. He is a frequent speaker on national and international software developer conferences in Germany, England and Poland. Daniel is actively contributing the software developer community as lead of a user group and organizer of the biggest regional software developer community event. You can read his blog by visiting lennybacon.com.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberconscab18 Jan '13 - 1:31 
GeneralRe: My vote of 5memberDaniel Fisher (lennybacon)18 Jan '13 - 2:26 
QuestionHELP me what to do nextmemberMember 97233272 Jan '13 - 7:54 
QuestionHELPmemberMember 97233272 Jan '13 - 7:53 
AnswerRe: HELPmemberDaniel Fisher (lennybacon)18 Jan '13 - 2:26 
Generalthe bestmemberEvgeniy_9213 Oct '10 - 10:27 
GeneralYour application in Window Form flavor!memberE! Ray K10 Sep '08 - 10:24 
GeneralAnother approach...memberDoodie Woddy Doodlechips16 Mar '08 - 3:51 
QuestionHow to convert from Ascii to Image [modified]membernaveensri13 Jul '06 - 2:12 
GeneralGrayscale, GetPixel method confusionmemberallapg21 Jun '05 - 17:08 
GeneralRe: Grayscale, GetPixel method confusionmemberDaniel Fisher (lennybacon)21 Jun '05 - 23:23 
First of all Daniel, please. And it would have been nice if you have provided your name Smile | :)
 
1. GetPixel() returns the color of one pixel of your image.
2. GetBrightness() returns the brightness of this color
 
I just use a factor (* 100) and add all values i get from each segment, which is is a group of pixels that will be converted to a character. the sum of that calculation is divided and the amount of the second calc is used to decide which character will be used.
 
clear? Come back to me if not, k.
 
Daniel
 
# THIS CODE AND INFORMATION ARE PROVIDED
# "AS IS" WITHOUT WARRANTY OF ANY
# KIND, EITHER EXPRESSED OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE
# IMPLIED WARRANTIES OF MERCHANTABILITY
# AND/OR FITNESS FOR A PARTICULAR PURPOSE.
# http://www.lennybacon.com/
GeneralRe: Grayscale, GetPixel method confusionmemberDaniel Fisher (lennybacon)21 Jun '05 - 23:24 
GeneralRe: Grayscale, GetPixel method confusionmemberallapg22 Jun '05 - 18:02 
GeneralRe: Grayscale, GetPixel method confusionmemberDaniel Fisher (lennybacon)23 Jun '05 - 2:39 
GeneralCrop Imagememberaaccoobb31 Mar '05 - 0:15 
GeneralRe: Crop ImagememberDaniel Fisher (lennybacon)21 Jun '05 - 23:25 
QuestionWhat kind of methods?memberSnowjade2 Mar '05 - 16:03 
AnswerRe: What kind of methods?memberDaniel Fisher (lennybacon)3 Mar '05 - 21:34 
GeneralAn improvement...memberMiguel Lopes14 Feb '05 - 0:26 
GeneralNow this is just too cool!memberGISnet29 Jan '05 - 19:19 
GeneralBeautiful programmemberPhan Dung27 Jan '05 - 15:25 
GeneralNice and can be perfectmemberkawzaki25 Jan '05 - 17:32 
GeneralRe: Nice and can be perfectmemberDaniel Fisher (lennybacon)26 Jan '05 - 1:15 
Generaladd in installmemberPyro Joe22 Jan '05 - 16:49 
GeneralRe: add in installmemberDaniel Fisher (lennybacon)22 Jan '05 - 23:23 
GeneralA little typo in your codememberEirik18 Jan '05 - 20:46 
GeneralRe: A little typo in your codememberDaniel Fisher (lennybacon)18 Jan '05 - 22:15 
Questionhow about html conversionsussnokiko12 Jan '05 - 22:53 
AnswerRe: how about html conversionsussMatthew Hanna26 Jun '05 - 8:09 
GeneralRe: how about html conversionmemberMatthew Hanna26 Jun '05 - 13:50 
Questionhow about html conversionmembernokiko12 Jan '05 - 22:53 
GeneralVery cool btw: I ported it to VB.netmemberPaul Farry5 Jan '05 - 20:45 
GeneralRe: Very cool btw: I ported it to VB.netmemberDaniel Fisher (lennybacon)6 Jan '05 - 0:30 
GeneralRe: Very cool btw: I ported it to VB.netsussPaul Farry6 Jan '05 - 8:17 
GeneralRe: Very cool btw: I ported it to VB.netsussAnonymous20 Jan '05 - 13:31 
GeneralRe: Very cool btw: I ported it to VB.netmemberDaniel Fisher (lennybacon)21 Jan '05 - 0:08 
GeneralRe: Very cool btw: I ported it to VB.netmemberlennybacon14 May '07 - 22:48 
GeneralFunmemberMike Ellison5 Jan '05 - 7:45 
GeneralOk, this is just cool....memberwebguy5531 Dec '04 - 11:29 
GeneralRe: Ok, this is just cool....memberDaniel Fisher (lennybacon)2 Jan '05 - 21:53 
GeneralIf you like theesememberJuMaCaBo29 Dec '04 - 4:15 
GeneralYou were MSDN-blogged :-)sitebuilderUwe Keim28 Dec '04 - 17:45 
GeneralRe: You were MSDN-blogged :-)memberDaniel Fisher (lennybacon)29 Dec '04 - 6:29 
Questionhow to install it?memberMr.Prakash27 Dec '04 - 21:48 
AnswerRe: how to install it?memberDaniel Fisher (lennybacon)28 Dec '04 - 0:37 
GeneralWhy not include the project file?memberSteven Fowler28 Dec '04 - 3:36 
GeneralRe: Why not include the project file?memberDaniel Fisher (lennybacon)28 Dec '04 - 14:49 
GeneralRe: Why not include the project file?sussSteven Fowler29 Dec '04 - 5:40 
GeneralRe: Why not include the project file?memberDaniel Fisher (lennybacon)29 Dec '04 - 6:35 
GeneralCool one!!!memberRajesh Pillai27 Dec '04 - 4:33 

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

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