473,728 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

- Default.aspx.cs

using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Drawing. Imaging;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
using System.Text;
using System.IO;

namespace AsciiArt
public class WebForm1 : System.Web.UI.P age
protected System.Web.UI.W ebControls.Text Box txtOutput;
protected System.Web.UI.W ebControls.Butt on btnGo;
protected System.Web.UI.H tmlControls.Htm lInputFile file;
protected System.Web.UI.W ebControls.Text Box txtInput;
private void Page_Load(objec t sender, System.EventArg s e)
if (!Page.IsPostBa ck)
StringBuilder input = new StringBuilder() ;
for (int i = 0; i < 2500; i++)
input.Append("b lah ");
txtInput.Text = input.ToString( );

private string ColorizeText(Sy stem.Drawing.Im age image, string
//determine how to scale the text
int textLength = input.Length;
int imageHeight = image.Height;
int imageWidth = image.Width;
double ratio = (double)imageHe ight / (double)imageWi dth;
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)(textLengt h / 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;

endPointer = input.LastIndex Of(" ",(startPoi nter +
rowText.Add(inp ut.Substring(st artPointer,endP ointer-startPointer));
startPointer = endPointer + 1;
while (startPointer+c olumns<=input.L ength);

if (startPointer<i nput.Length)
rowText.Add(inp ut.Substring(st artPointer));

using (Bitmap bitmap = new
Bitmap(image,co lumns,rowText.C ount))

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;");
Color color =
bitmap.GetPixel (colCtr,rowCtr) ;

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

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
// CODEGEN: This call is required by the ASP.NET Web Form
InitializeCompo nent();
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
this.btnGo.Clic k += new
System.EventHan dler(this.btnGo _Click);
this.Load += new System.EventHan dler(this.Page_ Load);


private void btnGo_Click(obj ect sender, System.EventArg s e)
using (System.Drawing .Image image =
System.Drawing. Image.FromStrea m(file.PostedFi le.InputStream) )
string colorizedText = ColorizeText(im age,
Response.Write( colorizedText);
txtOutput.Text = colorizedText;
catch (Exception ex)

- Default.aspx

<%@ Page language="c#" Codebehind="Def ault.aspx.cs"
AutoEventWireup ="false" validateRequest ="false"
Inherits="Ascii Art.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<title>colorize r.r.r.</title>
body {
font: Tahoma, Arial, sans-serif;
margin : 15px 0px 0px 15px;
background : #f3f4fb;
<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>
<LEGEND>1. Upload source image</LEGEND>
<INPUT type="file" runat="server" id="file">
<LEGEND>2. Enter text to be colorized</LEGEND>
<asp:TextBox id="txtInput" runat="server" Rows="10"
Columns="60" TextMode="Multi Line"></asp:TextBox>
<LEGEND>3. Delight in your glorious new HTML</LEGEND>
<asp:TextBox id="txtOutput" runat="server" Rows="10"
Columns="60" TextMode="Multi Line"></asp:TextBox></P>
<asp:Button id="btnGo" runat="server"
Nov 16 '05 #1
0 2178

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

Similar topics

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 retain the automatic inclusion of a graphic in place of hr. I have a 300 px graphic I'd like to continue using, without a manual search and replace of <hr> on 300 web pages.... Thanks,
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 System.Drawing.Bitmap from that value? Regards, Steve
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, EmfType.EmfOnly); grph.ReleaseHdc(ipHDC); grph.Dispose(); grph = Graphics.FromImage(mf); grph.DrawRectangle(aPen, 10, 10, 100, 120); grph.Dispose();
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 has an image of a house with a door in it and the user is interested in the door. the rectangle is selected by placing the mouse on the upper left corner of the door, pressing down on the left mouse button, and draging it to the lower right...
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 the bots that go out and process forms. The user has to type in the code they see, when the graphic itself should be unreadable by character recognition engines. So, how are the graphics generated, and how do you implement this?
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?
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? In my calling routine I have the following: *********************************** function OpenLogoPreviewWindow() { window.open('logoDisplay.htm','mywindow'); }
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, fGetPerformanceGrade(SalesPersonID as Long) as String to retrieve that salesperson's grade (e.g. A+, A, A-, B+, B... D). Also one other function, fGetMonthlySales(SalesPersonID as Long) as String to get their sales figure, e.g. "$87K". I want web visitors to be able to click...
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 colored dot to indicate the category of an item. I have been able to draw basic circles using the System.Drawing.Drawing2D namespace. What I want though is 'beautiful' icons (e.g. that look the the Windows Vista bottom-left Start button, save...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.