namespace ConvertImageFromUrlToAscii
private const string _remoteImageUrl = "http://www.tutsking.com/wp-content/uploads/disney-mickey-mouse-head.jpg";
private const int _asciiWidth = 150;
private static string[] _asciiChars = { "#", "#", "@", "%", "=", "+", "*", ":", "-", ".", " " };
public static void Main()
Bitmap image = GetBitmapFromUrl(_remoteImageUrl);
string ascii = ConvertImageToAsciiArt(image);
private static Bitmap GetBitmapFromUrl(string remoteImageUrl)
WebRequest request = WebRequest.Create(remoteImageUrl);
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
Bitmap bitmap = new Bitmap(responseStream);
private static string ConvertImageToAsciiArt(Bitmap image)
image = GetReSizedImage(image, _asciiWidth);
string ascii = ConvertToAscii(image);
private static Bitmap GetReSizedImage(Bitmap inputBitmap, int asciiWidth)
asciiHeight = (int) Math.Ceiling((double) inputBitmap.Height*asciiWidth/inputBitmap.Width);
Bitmap result = new Bitmap(asciiWidth, asciiHeight);
Graphics g = Graphics.FromImage((Image) result);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(inputBitmap, 0, 0, asciiWidth, asciiHeight);
private static string ConvertToAscii(Bitmap image)
StringBuilder sb = new StringBuilder();
for (int h = 0; h < image.Height; h++)
for (int w = 0; w < image.Width; w++)
Color pixelColor = image.GetPixel(w, h);
int red = (pixelColor.R + pixelColor.G + pixelColor.B)/3;
int green = (pixelColor.R + pixelColor.G + pixelColor.B)/3;
int blue = (pixelColor.R + pixelColor.G + pixelColor.B)/3;
Color grayColor = Color.FromArgb(red, green, blue);
int index = (grayColor.R*10)/255;
sb.Append(_asciiChars[index]);
sb.Append(Environment.NewLine);