Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 39,950 times

Related Categories

Embed text in Image using ASP.NET

julesr

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.

Look at this photo of myself. I can embed some text into the photo using the following code:

image_text.aspx

<%@ Page Language="c#"%>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<script runat="server">
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) ;
}
</script>

For a demo, take a look at http://www.charon.co.uk/demos/image_text.aspx?i=/file_library/images/articles/jules_photo.jpg&t=Photo+of+Jules. Play around with the querystring. See how the caption changes when you change the text in the querystring.

I've been a freelance web developer since Feb '00. Prior to that, I'd spent most of my working life in the pub trade. I'd also worked as a security guard in a local factory. Luckily, I was able to my laptop into work. It was there that I used to spend my 12 hour shifts learning about web development! Over the years, I have worked on a lot of interesting sites. You can see a list of more recent sites in my portfolio. And acquired a string set of technical skillsets. These include: * ASP/VB Script * ASP.NET/C# * SQL Server 2000/Access 2000 * XML/XSLT It should be noted that my design skills are practically zero. Jojo, from the DW newsgroup, did the design for this site. I just put it all together in Dreamweaver MX. I don't really have much time for hobbies or pastimes. When I do get some spare time, I enjoy playing scrabble, doing crosswords and playing golf. At one time, I got down to a single-figure handicap at golf. But, I'd struggle to maintain that now!

Comments