Home  


Creating Text Image on the fly in ASP.NET

Some time back I had a requirement where I had to create the Text Images dynamically. I check the net for the same but then I could not find any sample code, so I started on my own and now here is the code to generate the Text image on the fly in ASP.net.

					
public void Page_Load(object sender, System.EventArgs e)
{
	if (Request.HttpMethod == "POST")
	{
		string text = Request.Form.Get("text");
		int textLength = text.Length;
		int fontSize = Convert.ToInt32(Request.Form.Get("fontsize"));
		int orientation = Convert.ToInt32(Request.Form.Get("orientation"));
		int antialias = Convert.ToInt32(Request.Form.Get("antialias"));

		// Set canvas width & height
		int width; 
		int height;
		if (orientation == 1)
		{
			width  = (fontSize * textLength) - ((textLength * fontSize)/3);
			height = fontSize + 20;
		}
		else
		{
			width  = fontSize + 20;
			height = (int)(fontSize * textLength * 1.5);
		}

		// Initialize graphics
		RectangleF rectF =new RectangleF(0, 0, width, height);
		Bitmap pic = new Bitmap(width, height, PixelFormat.Format24bppRgb);
		Graphics g = Graphics.FromImage(pic);
		g.SmoothingMode = SmoothingMode.AntiAlias;
		if (antialias == 1) g.TextRenderingHint = TextRenderingHint.AntiAlias;
	
		// Set colors
		string fgColor = Request.Form.Get("fontcolor");
		string bgColor = Request.Form.Get("bgcolor");
		Color fontColor = Color.FromName(fgColor);
		Color rectColor = Color.FromName(bgColor);
		SolidBrush fgBrush = new SolidBrush(fontColor);
		SolidBrush bgBrush = new SolidBrush(rectColor);
		
		// Rectangle or ellipse?
		int bound = Convert.ToInt32(Request.Form.Get("bound"));
		if (bound == 1)
		{
			g.FillRectangle(bgBrush, rectF);
		}
		else
		{
			g.FillRectangle(new SolidBrush(Color.White), rectF);
			g.FillEllipse(bgBrush, rectF);
		}
	
		// Load font			
		string fontName = Request.Form.Get("fontname") + ".ttf";
		PrivateFontCollection privateFontCollection = new PrivateFontCollection();
		privateFontCollection.AddFontFile(Server.MapPath("./") + fontName);
	
		FontFamily fontFamily = privateFontCollection.Families[0];

		// Set font style
		int fontStyle = Convert.ToInt32(Request.Form.Get("fontstyle"));
		FontStyle style = FontStyle.Regular;
		switch (fontStyle)
		{
			case 2:
				style = FontStyle.Bold;
				break;
			case 3:
				style = FontStyle.Italic;
				break;
			case 4:
				style = (FontStyle.Bold) | (FontStyle.Italic);
				break;
			case 5:
				style = FontStyle.Strikeout;
				break;
			case 6:
				style = FontStyle.Underline;
				break;
		}
		Font font = new Font(fontFamily, fontSize, style, GraphicsUnit.Pixel);

		// Set font direction & alignment
		StringFormat format = new StringFormat();
		int reverse = Convert.ToInt32(Request.Form.Get("reverse"));
		if (reverse == 1 && orientation == 1)
		{
			format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
		}
		else if (reverse == 1 && orientation > 1)
		{
			StringBuilder temp = new StringBuilder();
			for (int i = textLength-1; i >= 0; i--)
			{
				temp.Insert((textLength-1) - i, text[i]);
			}
			text = temp.ToString();
		}

		if (orientation > 1)
		{
			rectF.X = width/4;
			rectF.Width = fontSize - (fontSize/4);
		}

		int alignment = Convert.ToInt32(Request.Form.Get("alignment"));
		if (alignment == 1)
		{
			format.Alignment = StringAlignment.Near;
		}
		else if (alignment == 2)
		{
			format.Alignment = StringAlignment.Center;
		}
		else
		{
			format.Alignment = StringAlignment.Far;
		}
		format.LineAlignment = StringAlignment.Center;
	
		// Draw any drop-shadow
		int dropShadow = Convert.ToInt32(Request.Form.Get("dropshadow"));
		if (dropShadow > 0)
		{
		Color shadowColor = Color.FromName(Request.Form.Get("shadowcolor"));
		switch(dropShadow)
		{
		case 1:
			rectF.Offset(-3, -3);
			g.DrawString(text, font, new SolidBrush(shadowColor), rectF, format);
			rectF.Offset(+3, +3);
			break;
			
		case 2:
			rectF.Offset(+3, -3);
			g.DrawString(text, font, new SolidBrush(shadowColor), rectF, format);
			rectF.Offset(-3, +3);
			break;
			
		case 3:
			rectF.Offset(-3, +3);
			g.DrawString(text, font, new SolidBrush(shadowColor), rectF, format);
			rectF.Offset(+3, -3);
			break;

		case 4:
			rectF.Offset(+3, +3);
			g.DrawString(text, font, new SolidBrush(shadowColor), rectF, format);
			rectF.Offset(-3, -3);
			break;
		}
		}
		
		// Finally, draw the font
		g.DrawString(text, font, fgBrush, rectF, format);

		Response.ContentType = "image/jpeg";
		pic.Save(Response.OutputStream, ImageFormat.Jpeg);

		// Dispose objects
		pic.Dispose();
	}
}


					
					

The Output image generated from the above code

..full code Download here