Monday, October 6, 2008

Embed text in Image using ASP.NET

Sometimes, it is nice to have a text caption embedded into an image, rather than display the caption in HTML. Fortunately, this is fairly straightforward in ASP.NET.

image_text.aspx

<%@ Page Language="c#"%>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>

private void Page_Load(object sender, System.EventArgs e)
{
  Bitmap bmp= new Bitmap(Server.MapPath(Request.QueryString["i"]));
  Graphics g=Graphics.FromImage(bmp);
  g.SmoothingMode = SmoothingMode.AntiAlias ;
  g.DrawString(Request.QueryString["t"],
  new Font("verdana",12),SystemBrushes.WindowText, 1, 1);
  Response.ContentType="image/jpeg";
  bmp.Save(Response.OutputStream, bmp.RawFormat) ;
}

No comments: