Last active
October 14, 2015 20:23
-
-
Save vardars/3e7e1316fe5ab215519f to your computer and use it in GitHub Desktop.
Text to Ascii
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Text; | |
namespace AsciiPainter | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
Console.WriteLine("Yazıyı giriniz:"); | |
var input = Console.ReadLine(); | |
var inputImage = DrawText(input, new Font(FontFamily.GenericMonospace, 12), Color.Black, Color.White); | |
var outputText = GrayscaleImageToASCII(inputImage); | |
Console.Clear(); | |
Console.Write(outputText); | |
Console.WriteLine("Kapatmak için bir tuşa basınız."); | |
Console.ReadKey(); | |
} | |
private static Image DrawText(String text, Font font, Color textColor, Color backColor) | |
{ | |
//first, create a dummy bitmap just to get a graphics object | |
Image img = new Bitmap(1, 1); | |
Graphics drawing = Graphics.FromImage(img); | |
//measure the string to see how big the image needs to be | |
SizeF textSize = drawing.MeasureString(text, font); | |
//free up the dummy image and old graphics object | |
img.Dispose(); | |
drawing.Dispose(); | |
//create a new image of the right size | |
img = new Bitmap((int)textSize.Width, (int)textSize.Height); | |
drawing = Graphics.FromImage(img); | |
//paint the background | |
drawing.Clear(backColor); | |
//create a brush for the text | |
Brush textBrush = new SolidBrush(textColor); | |
drawing.DrawString(text, font, textBrush, 0, 0); | |
drawing.Save(); | |
textBrush.Dispose(); | |
drawing.Dispose(); | |
return img; | |
} | |
public static string GrayscaleImageToASCII(Image img) | |
{ | |
StringBuilder html = new StringBuilder(); | |
Bitmap bmp = null; | |
try | |
{ | |
bmp = new Bitmap(img); | |
for (int y = 0; y < bmp.Height; y++) | |
{ | |
for (int x = 0; x < bmp.Width; x++) | |
{ | |
Color col = bmp.GetPixel(x, y); | |
col = Color.FromArgb((col.R + col.G + col.B) / 3, | |
(col.R + col.G + col.B) / 3, | |
(col.R + col.G + col.B) / 3); | |
int rValue = int.Parse(col.R.ToString()); | |
html.Append(getGrayShade(rValue)); | |
if (x == bmp.Width - 1) | |
html.Append(Environment.NewLine); | |
} | |
} | |
return html.ToString(); | |
} | |
catch (Exception exc) | |
{ | |
return exc.ToString(); | |
} | |
finally | |
{ | |
bmp.Dispose(); | |
} | |
} | |
private static string getGrayShade(int redValue) | |
{ | |
if (redValue >= 230) | |
return " "; | |
else | |
return "*"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tek başına compile etmek için: