VB icon

Dynamic Text Image Creation

Email
Submitted on: 1/2/2015 9:35:00 PM
By: Daniel M. Hendricks (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: C#, ASP.NET
Views: 1750
 
     Create text graphics on the fly using GDI+. Supports any TrueType font and allows multiple style options. See comments for usage. Please vote or post questions.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Dynamic Text Image Creation
// Description:Create text graphics on the fly using GDI+. Supports any TrueType font and allows multiple style options. See comments for usage. Please vote or post questions.
// By: Daniel M. Hendricks (from psc cd)
//**************************************

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Text" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
 
<%
/* USAGE:
// script.aspx?<parameters...>
// 
// VALID PARAMETERS:
// text = The text to display
// color = The Hex color of the text
// bgcolor = The hex background color
// font = Name of truetype font
// size = Size of the font in points
// bold = If not null, bold
// italic = If not null, italicized
// strikeout = If not null, striked
// underline = If not null, underlined
// antialias = If not null, anti-aliased
// 
// EXAMPLE USAGE:
// script.aspx?text=Hello&size=16
// script.aspx?text=Hello&bold=true
// 
// REQUIREMENTS:
// Either change the 'fontDir' variable
// to the location your TrueType fonts
// are stored in, or copy your fonts to
// the same folder as the script (ie,
// arial.ttf, tahoma.ttf, etc).
*/
 
// SET VARIABLE VALUES
string fontDir = Server.MapPath("./");
string text = ((Request.QueryString["text"] == null) ? "Default Text" : Request.QueryString["text"].ToString() );
string fgColor = ((Request.QueryString["color"] == null) ? "#000000" : "#"+Request.QueryString["color"].ToString() );
string bgColor = ((Request.QueryString["bgcolor"] == null) ? "#FFFFFF" : "#"+Request.QueryString["bgcolor"].ToString() );
string fontName = ((Request.QueryString["font"] == null) ? "arial" : Request.QueryString["font"].ToString() );
int fontSize = ((Request.QueryString["size"] == null) ? 12 : Convert.ToInt32(Request.QueryString["size"]) );
 
// LOAD TRUETYPE FONT
PrivateFontCollection privateFontCollection = new PrivateFontCollection();
privateFontCollection.AddFontFile(fontDir + fontName + ".ttf");
FontFamily fontFamily = privateFontCollection.Families[0]; 
 
// SET FONT STYLE
FontStyle style = FontStyle.Regular;
if(Request.QueryString["bold"] != null) style = style | FontStyle.Bold;
if(Request.QueryString["italic"] != null) style = style | FontStyle.Italic;
if(Request.QueryString["strikeout"] != null) style = style | FontStyle.Strikeout;
if(Request.QueryString["underline"] != null) style = style | FontStyle.Underline;
Font font = new Font(fontFamily, fontSize, style, GraphicsUnit.Pixel); 
 
// INITIALIZE GRAPHICS
Bitmap img = new Bitmap(1,1);
Graphics g = Graphics.FromImage(img);
 
// DETERMINE CANVAS WIDTH AND HEIGHT
SizeF stringLength = g.MeasureString(text, font, 300);
int width = Convert.ToInt32(stringLength.Width);
int height = fontSize;
 
// SET COLORS
SolidBrush fgBrush = new SolidBrush(ColorTranslator.FromHtml(fgColor));
SolidBrush bgBrush = new SolidBrush(ColorTranslator.FromHtml(bgColor));
 
// SET GRAPHIC OBJECT
RectangleF rectF = new RectangleF(-1, -1, width+1, height+1);
img = new Bitmap(width, height, PixelFormat.Format24bppRgb);
g = Graphics.FromImage(img);
g.SmoothingMode = SmoothingMode.AntiAlias;
if (Request.QueryString["antialias"] != null) g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.FillRectangle(bgBrush, rectF);
 
// SET ALIGNMENT
StringFormat format = new StringFormat();
format.LineAlignment = StringAlignment.Center;
 
// DRAW THE FONT
MemoryStream memStream = new MemoryStream();
g.DrawString(text, font, fgBrush, rectF, format); 
Response.ContentType = "image/png";
img.Save(memStream, ImageFormat.Png);
memStream.WriteTo(Response.OutputStream);
 
// CLEAN UP
img.Dispose();
%>


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.