CSharp · Console
// http://npg.si.edu/sites/default/files/blog_obama_martin_schoeller.jpg
using System;
using System.Net;
using System.IO;
using System.Drawing;
public class Program
{
private static string letter { get; set; }
private static int LineWidth { get; set; }
public static void Main()
{
GetUserInput();
//var image = GetImage(URL);
var image = GetLetterImage(letter);
Bitmap bitmap = ResizImage(image, LineWidth);
ConvertToAscii(bitmap);
}
private static void GetUserInput()
{
Console.Write("Enter the letter?");
letter = Console.ReadLine();
Console.Write("Enter Line Character Amount");
LineWidth = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
}
private static Image GetImage(string url)
{
using (var client = new WebClient())
{
byte[] data = client.DownloadData(url);
using (var ms = new MemoryStream(data))
{
return Image.FromStream(ms);
}
}
}
private static Image GetLetterImage(string letter)
{
Stream imageStream = new MemoryStream();
using (Bitmap board = new Bitmap(250,300))
using (var g=Graphics.FromImage(board))
using(Font drawFont = new Font("Arial", 36))
{
//g.Clear(Color.White);
g.DrawString(letter,drawFont,System.Drawing.Brushes.White,0,0);
board.Save(imageStream,System.Drawing.Imaging.ImageFormat.Jpeg);
}
return Image.FromStream(imageStream);;
}
private static void ConvertToAscii(Bitmap bitmap)
{
char[] ascii = { '#', '@', '%', 'W', 'w', '=', '+', '*', ':', '-', ',', '.', ' ' };
for (var h = 0; h < bitmap.Height; h++)
{
for (var w = 0; w < bitmap.Width; w++)
{
var pixel = bitmap.GetPixel(w, h);
int gray = (pixel.R + pixel.G + pixel.B) / 3;
int alpha = pixel.A == 255 ? 1 : 0;
int index = (int)(gray * ascii.Length / 255);
Console.Write(ascii[(alpha == 0 ? ascii.Length - 1 : index)]);
}
Console.WriteLine();
}
}
private static Bitmap ResizImage(Image image, int width)
{
Bitmap bitmap = new Bitmap(image);
int height = (int)Math.Ceiling((double)(bitmap.Height * width) / bitmap.Width);
return new Bitmap(bitmap, width, (height / 2) + (width / 4));
}
}