Mission:
Create application that makes ASCII art from pictures.
How does it work:
Press "Convert" button, open your image, save your HTML file. Default width symbols are 200 and it is optimal symbol count for quality. You can choose different width symbol count yourself.
It's taking every your image, makes it black and white. Then it applies symbols from black to white.
Screenshot:
http://img213.imageshack.us/img213/460/screenrf.png
Virus Check:
http://www.virustotal.com/analisis/5...267-1263833816
Source Code
(I won't share my project files. All you need is here.)
Main Form
PHP Code:
#region Using...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Img2ASCIIConverter.Classes;
using System.Threading;
using System.IO;
#endregion
namespace Img2ASCIIConverter
{
public partial class frmMainForm : Form
{
//Global variables.
StringBuilder ASCIICode = null;
int ImageQuality;
public frmMainForm()
{
InitializeComponent();
}
private void buttonConvert_Click(object sender, EventArgs e)
{
ImageQuality = trackBarQuality.Value;
openFileDialogImage.ShowDialog();
//Creating our thread so that it won't lag. (Just in case)
Thread ConvertingThread = new Thread(StartConverting);
//ApartmentState = STA. Because we need to open file save dialogue after thread is done.
ConvertingThread.SetApartmentState(ApartmentState.STA);
//Starting our thread! :)
ConvertingThread.Start();
}
private void StartConverting()
{
//We TRY because some errors might appear if we select MP3 file for example.
try
{
Bitmap bmp = new Bitmap(openFileDialogImage.FileName);
ASCIICode = new StringBuilder();
//We start the class method but first we convert our image to our desierd quality (size).
ASCIICode.Append(Img2ASCII.Convert(Img2ASCII.GetReSizedImage(bmp, ImageQuality)));
//Annoying message showing that we succeeded. :)
MessageBox.Show("Converting process done!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
//Opening our save dialog to save our HTML file.
saveFileDialogASCIICode.ShowDialog();
}
//Don't want anything to be shown, but still it REQ to catch something 0.o
catch
{
}
}
private void saveFileDialogASCIICode_FileOk(object sender, CancelEventArgs e)
{
//Saving and closing...
TextWriter ASCIIFile = new StreamWriter(saveFileDialogASCIICode.FileName);
ASCIIFile.Write(ASCIICode.ToString());
ASCIIFile.Close();
}
}
}
Img2ASCII.cs
PHP Code:
/*
************************************************************************************
* Class that converts PICTURES to ASCII code. *
* by Steve Moss (Qmal) *
* Dr.Qmal@gmail.com *
* *
* Some code parts are taken from MSDN. Some are improved. Not 100% my work. *
* With Love, From Qmal. *
************************************************************************************
*/
#region Using...
using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
#endregion
namespace Img2ASCIIConverter.Classes
{
class Img2ASCII
{
public static string Convert(Image img)
{
//Our String Builder and Bitmap.
StringBuilder html = new StringBuilder();
Bitmap bmp = null;
try
{
//Creating new Bitmap out of our Image.
bmp = new Bitmap(img);
//Setting font size to smallest and fonts to Terminal. (HTML Code)
html.Append("<font size='1' face='Terminal'>");
//Going through every pixel in image.
//Making whole image Black&White.
//I could use Matrix method which is faster and better, but I'm not so good at Mathematics (Junior) so I pass.
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
//Getting the color of current pixel.
Color col = bmp.GetPixel(x, y);
//Making our image Black&White
col = Color.FromArgb((col.R + col.G + col.B) / 3, (col.R + col.G + col.B) / 3, (col.R + col.G + col.B) / 3);
//Brightness Value.
int bValue = col.R;
//Applying ASCII character.
html.Append(getASCIIChar(bValue));
//If our image width pixels are over we are making a break line in text.
if (x == bmp.Width - 1)
html.Append("</br>");
}
}
//Finnaly I can close off HTML code.
html.Append("</font>");
//Returning our ASCII drawing.
return html.ToString();
}
catch (Exception e)
{
//Showing error message if something went wrong...
MessageBox.Show("Something went wrong...", "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return e.ToString();
}
finally
{
//Disposing our image.
bmp.Dispose();
}
}
private static string getASCIIChar(int bValue)
{
//Simple stuff here.
//Assinging characters to level of Brightness of pixel.
string ASCIIChar;
if (bValue >= 230)
{
ASCIIChar = " ";
}
else if (bValue >= 200)
{
ASCIIChar = ".";
}
else if (bValue >= 180)
{
ASCIIChar = ",";
}
else if (bValue >= 160)
{
ASCIIChar = ":";
}
else if (bValue >= 130)
{
ASCIIChar = "o";
}
else if (bValue >= 100)
{
ASCIIChar = "&";
}
else if (bValue >= 70)
{
ASCIIChar = "8";
}
else if (bValue >= 50)
{
ASCIIChar = "#";
}
else
{
ASCIIChar = "@";
}
return ASCIIChar;
}
public static Bitmap GetReSizedImage(Bitmap inputBitmap, int asciiWidth)
{
//Converting our image.
//I could use some other methods without converting image, but this is easiest I think.
//I took this frome somewhere.
//MSDN Probably. :)
//Calculate the new Height of the image from its width
int 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;
}
}
}