ASP.net is getting to be a lot cooler as I find out more about what I can do. I’m now dynamically creating the text headers for each blog entry. And it was really simple to do. Here’s how it’s done.

SizeF mySize;
Bitmap imageForText;
Font drawFont = new Font(“AvantGarde BK BT”,16,FontStyle.Bold);
imageForText = new Bitmap(500,30,PixelFormat.Format32bppPArgb);
Graphics myGraphics;
myGraphics = Graphics.FromImage(imageForText);

mySize = myGraphics.MeasureString(text,drawFont);
myGraphics.Clear(Color.White);
myGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//Makes a shadow
myGraphics.DrawString(text,drawFont,new SolidBrush(Color.FromArgb(229,229,229)),2,2);
myGraphics.DrawString(text,drawFont,new SolidBrush(Color.FromArgb(140,31,57)),0,0);

imageForText.Save(Response.OutputStream,ImageFormat.Jpeg);
try{
imageForText.Save(Server.MapPath(“images/textheaders/”)+
text+”.jpg”,ImageFormat.Jpeg);
}
catch(Exception exc){}

Response.Flush();
//Clean Up
imageForText.Dispose();

The try ... catch block is used in case there is a funky character in the title that can’t be saved to file, then I just ignore it.

Nice eh?

Comments are closed.