473,378 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to create a colored ASCII art from a colored graphic image

> I have a graphics images that I want to convert to
ASCII art. How do I do it?

Code:
- Default.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;

namespace AsciiArt
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtOutput;
protected System.Web.UI.WebControls.Button btnGo;
protected System.Web.UI.HtmlControls.HtmlInputFile file;
protected System.Web.UI.WebControls.TextBox txtInput;
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
StringBuilder input = new StringBuilder();
for (int i = 0; i < 2500; i++)
{
input.Append("blah ");
}
txtInput.Text = input.ToString();
}
}

private string ColorizeText(System.Drawing.Image image, string
input)
{
//determine how to scale the text
int textLength = input.Length;
int imageHeight = image.Height;
int imageWidth = image.Width;
double ratio = (double)imageHeight / (double)imageWidth;
StringBuilder output = new StringBuilder();

//letters are about twice as tall as wide; correct
ratio /= 2;

//height * width = text
//height / width = ratio
//width = text / height
//width = height / ratio
//height / ratio = length / height
//height^2 = ratio * text
//height = sqrt (ratio * text)
int rows = (int)(Math.Sqrt(ratio * textLength));
int columns = (int)(textLength / rows);

//Chop the text into rows, since the word breaks will
cause this to be a little
//uneven so the exact row count is variable.
string inputBuffer = input;
ArrayList rowText = new ArrayList();
int startPointer = 0;
int endPointer = columns;

do
{
endPointer = input.LastIndexOf(" ",(startPointer +
columns));
rowText.Add(input.Substring(startPointer,endPointe r-startPointer));
startPointer = endPointer + 1;
}
while (startPointer+columns<=input.Length);

if (startPointer<input.Length)
rowText.Add(input.Substring(startPointer));

using (Bitmap bitmap = new
Bitmap(image,columns,rowText.Count))
{

output.Append("<span style='font-family: Lucinda
Console,Courier New;font-size: 8px;'>");

int rowCtr = 0;
int colCtr = 0;

//Iterate the rows
foreach (string row in rowText)
{
colCtr = 0;

//Iterate each character in the row
foreach (char letter in row.ToCharArray())
{
if (letter == ' ')
{
output.Append("&nbsp;");
}
else
{
Color color =
bitmap.GetPixel(colCtr,rowCtr);

//If the pizel is white, darken it up a
bit.
if (color.GetBrightness() > .8)
output.AppendFormat("<font
color='#{0:X2}{1:X2}{2:X2}'>{3}</font>", (int)(.75*color.R),
(int)(.75*color.G), (int)(.75*color.B), letter);
else
output.AppendFormat("<font
color='#{0:X2}{1:X2}{2:X2}'>{3}</font>", color.R, color.G, color.B,
letter);
}
colCtr++;
}
output.Append("<br>");
rowCtr++;
}
}
output.Append("</span>");
return output.ToString();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnGo.Click += new
System.EventHandler(this.btnGo_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnGo_Click(object sender, System.EventArgs e)
{
try
{
using (System.Drawing.Image image =
System.Drawing.Image.FromStream(file.PostedFile.In putStream))
{
string colorizedText = ColorizeText(image,
txtInput.Text);
Response.Write(colorizedText);
txtOutput.Text = colorizedText;
}
}
catch (Exception ex)
{
}
}
}
}

- Default.aspx

<%@ Page language="c#" Codebehind="Default.aspx.cs"
AutoEventWireup="false" validateRequest="false"
Inherits="AsciiArt.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>colorizer.r.r.</title>
<style>
body {
font: Tahoma, Arial, sans-serif;
margin : 15px 0px 0px 15px;
background : #f3f4fb;
}
</style>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<h3>A slightly different take on the ASCII Art thing. This
takes your block of ascii text and colorizes it to match the colors in
the image you upload. Makes more sense when you see it in action -
give it a try:</h3>
<FIELDSET>
<LEGEND>1. Upload source image</LEGEND>
<INPUT type="file" runat="server" id="file">
</FIELDSET>
<br>
<FIELDSET>
<LEGEND>2. Enter text to be colorized</LEGEND>
<asp:TextBox id="txtInput" runat="server" Rows="10"
Columns="60" TextMode="MultiLine"></asp:TextBox>
</FIELDSET>
<br>
<FIELDSET>
<LEGEND>3. Delight in your glorious new HTML</LEGEND>
<asp:TextBox id="txtOutput" runat="server" Rows="10"
Columns="60" TextMode="MultiLine"></asp:TextBox></P>
</FIELDSET>
<asp:Button id="btnGo" runat="server"
Text="Go"></asp:Button></P>
</form>
</body>
</HTML>
Nov 16 '05 #1
0 2142

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Charax | last post by:
Is it possible to use CSS to define <hr> as a graphic image? FrontPage 2003 has apparently dropped the graphic hr from their themes and I am becoming more involved in using CSS, but would like to...
5
by: Steve Amey | last post by:
Hi all I have an ARGB value for a Colour (Eg. -65536. The value was retrieved by using the Color.ToArgb method), is there any way that I can create a System.Drawing.Image or a...
3
by: Richard Skopal | last post by:
In .NET Windows forms I can create a metafile using this code: Graphics grph = aControl.CreateGraphics(); IntPtr ipHDC = grph.GetHdc(); Metafile mf = new Metafile(aImgFilePath, ipHDC,...
1
by: ray well | last post by:
hi, i need to give the user the ability to extract a rectangular area of their choice from a graphic displayed in a picture box to the clipboard, so they can use it elsewhere. say the graphic...
2
by: Larry Bud | last post by:
Ok, I don't know what they're called, but you know the form: The page displays a "code", usually on a similar color background (dark gray), and the text is all squiggly. It's an attempt to foil...
4
by: Iacopo.Marmo | last post by:
Hi! I need to manipulate multicolor strings, i.e. strings with a color associated with each letter. Any suggestions?
2
by: tshad | last post by:
I have an aspx window that is going to open a window (javascript) and display a graphic. I want to resize the window to the size of the graphic before it actually displays - how do I do that? ...
11
by: Mark B | last post by:
I want to display a pre-designed graphical 'performance badge' on certain webpages (round, about 2cm diameter) next to a salesperson's details. I have a function,...
1
by: Mark B | last post by:
I am trying to programmatically create some icons for an Office 2007 add-in (VB/C#) that will appear on the navigation Ribbon. The particular icon is simply a colored round circle representing a...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.