Introduction
I always liked ASCII Art and my passion to generate one urged me to explore the GDI+ in .NET framework. In this article I'll show you how to generate ASCII art from a JPEG/Bitmap image in quick three simple steps.
- Load and Resize the Image.
- Read each pixel, get its color and convert them to its equivalent grayscale.
- Assign appropriate characters to each pixel based on the computed grayscale value.
Let's get down to Code
Start by initializing the System.Drawing.Bitmap class with the image path. This class is used to work with images defined by pixel data. Resize the image to a lower resolution while maintaining its aspect ratio. This step is crucial because each pixel of an image represents a character in the final ASCII output. For example, if the resolution of a JPEG image that you want to convert is 2592x1944, the resulting text would have 1944 lines of 2592 characters each and definitely you don't want one that big. Next, define a width for the ASCII image and determine the height by proportionately scaling it down.
scalingFactor = asciiWidth / imageWidth
asciiHeight = imageHeight * scalingFactor
private void btnConvertToAscii_Click(object sender, EventArgs e)
{
btnConvertToAscii.Enabled = false;
//Load the Image from the specified path
Bitmap image = new Bitmap(txtPath.Text, true);
//Resize the image...
//I've used a trackBar to emulate Zoom In / Zoom Out feature
//This value sets the WIDTH, number of characters, of the text image
image = GetReSizedImage(image,this.trackBar.Value);
//Convert the resized image into ASCII
_Content = ConvertToAscii(image);
//Enclose the final string between <pre> tags to preserve its formatting
//and load it in the browser control
browserMain.DocumentText = "<pre>" + _Content + "</pre>";
btnConvertToAscii.Enabled = true;
}
Initialize a Bitmap image object by specifying the new Width and Height. Create a graphics object from the new image object. Set the interpolation mode associated with the graphics object to HighQualityBicubic. This mode produces the highest quality transformed images (refer MSDN). You can also try changing the interpolation modes to either HighQualityBilinear or NearestNeighbor within the code and check your output.
private Bitmap GetReSizedImage(Bitmap inputBitmap, int asciiWidth )
{
int asciiHeight=0;
//Calculate the new Height of the image from its width
asciiHeight = (int)Math.Ceiling((double)inputBitmap.Height * asciiWidth / inputBitmap.Width);
//Create a new Bitmap and define its resolution
Bitmap result = new Bitmap(asciiWidth, asciiHeight);
Graphics g = Graphics.FromImage((Image)result);
//The interpolation mode produces high quality images
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(inputBitmap, 0, 0, asciiWidth, asciiHeight);
g.Dispose();
return result;
}

Saloni KapoorPosted Jun 23, 2015, 6:04 PM
using this code can we detect any unknown image is 0-9 ?
Tgp KuyaPosted Feb 9, 2015, 8:45 PM
TNX BRO
musium asdeditedPosted Aug 17, 2012, 3:10 PMEdited Aug 17, 2012, 3:11 PM
It could be simplified to: StringBuilder sb = new StringBuilder(); for (int i = 0; i < image.Height; i++) {for (int c = 0; c < image.Width; c++){Color pixelColor = image.GetPixel(c, i);if(i % 2 == 0)sb.Append(_chars[((pixelColor.R + pixelColor.G + pixelColor.B) / 3) * 10 / 255]);}if(i % 2 == 0)sb.Append("<BR>"); } return sb.ToString();
roshan meduriPosted Oct 30, 2007, 9:01 AM
dude i need a c code for displaying the objects present in a bitmap image and neglecting its background colour. please do help me urgent.. mail id is [email protected]
Me ThesamePosted Jun 8, 2007, 5:42 AM
Check out http://www.photo2text.com
sf fsdPosted Apr 14, 2007, 8:32 AM
example site http://photo2text.com
Ashok RajaPosted Mar 15, 2007, 7:09 AM
Really Great Man !!!
Ashok RajaPosted Mar 15, 2007, 7:06 AM
Really Great Man !!!
Dilli GrgPosted Mar 6, 2007, 5:56 PM
Man!!!You are good!!! :)