Click here to Skip to main content
6,597,576 members and growing! (20,323 online)
Email Password   helpLost your password?
Web Development » Charts, Graphs and Images » Images and multimedia     Intermediate

ASCII art with C#

By Daniel Fisher (lennybacon)

About writing an image to ASCII converter.
C#, VB.NET 1.0, .NET 1.1, .NET 2.0, Win2K, WinXP, Win2003, ASP.NET, GDI+, WebForms, VS.NET2003, VS2005, Dev
Posted:26 Dec 2004
Updated:31 Jan 2005
Views:248,246
Bookmarked:151 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
70 votes for this article.
Popularity: 8.76 Rating: 4.75 out of 5
1 vote, 1.4%
1
2 votes, 2.9%
2
2 votes, 2.9%
3
10 votes, 14.3%
4
55 votes, 78.6%
5

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)


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.
Occupation: Web Developer
Location: Germany Germany

Other popular Charts, Graphs and Images articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 46 (Total in Forum: 46) (Refresh)FirstPrevNext
GeneralYour application in Window Form flavor! PinmemberE! Ray K11:24 10 Sep '08  
GeneralAnother approach... PinmemberDoodie Woddy Doodlechips4:51 16 Mar '08  
GeneralHow to convert from Ascii to Image [modified] Pinmembernaveensri3:12 13 Jul '06  
GeneralGrayscale, GetPixel method confusion Pinmemberallapg18:08 21 Jun '05  
GeneralRe: Grayscale, GetPixel method confusion PinmemberDaniel Fisher (lennybacon)0:23 22 Jun '05  
GeneralRe: Grayscale, GetPixel method confusion PinmemberDaniel Fisher (lennybacon)0:24 22 Jun '05  
GeneralRe: Grayscale, GetPixel method confusion Pinmemberallapg19:02 22 Jun '05  
GeneralRe: Grayscale, GetPixel method confusion PinmemberDaniel Fisher (lennybacon)3:39 23 Jun '05  
GeneralCrop Image Pinmemberaaccoobb1:15 31 Mar '05  
GeneralRe: Crop Image PinmemberDaniel Fisher (lennybacon)0:25 22 Jun '05  
GeneralWhat kind of methods? PinmemberSnowjade17:03 2 Mar '05  
GeneralRe: What kind of methods? PinmemberDaniel Fisher (lennybacon)22:34 3 Mar '05  
GeneralAn improvement... PinmemberMiguel Lopes1:26 14 Feb '05  
GeneralNow this is just too cool! PinmemberGISnet20:19 29 Jan '05  
GeneralBeautiful program PinmemberPhan Dung16:25 27 Jan '05  
GeneralNice and can be perfect Pinmemberkawzaki18:32 25 Jan '05  
GeneralRe: Nice and can be perfect PinmemberDaniel Fisher (lennybacon)2:15 26 Jan '05  
Generaladd in install PinmemberPyro Joe17:49 22 Jan '05  
GeneralRe: add in install PinmemberDaniel Fisher (lennybacon)0:23 23 Jan '05  
GeneralA little typo in your code PinmemberEirik21:46 18 Jan '05  
GeneralRe: A little typo in your code PinmemberDaniel Fisher (lennybacon)23:15 18 Jan '05  
Generalhow about html conversion Pinsussnokiko23:53 12 Jan '05  
GeneralRe: how about html conversion PinsussMatthew Hanna9:09 26 Jun '05  
GeneralRe: how about html conversion PinmemberMatthew Hanna14:50 26 Jun '05  
Generalhow about html conversion Pinmembernokiko23:53 12 Jan '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 31 Jan 2005
Editor: Smitha Vijayan
Copyright 2004 by Daniel Fisher (lennybacon)
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project