I recently worked on an aspx dynamic image generator in C #, which in its most basic state takes the querystring value from "t" and writes this string to the image, and then returns a jpg.
It worked flawlessly locally, and on a test server — images returned instantly.
But when on load-balanced live servers it sometimes works fine. But more often than often, time is running out / takes a minute to return the image.
I thought I was asking here if there is something obvious in my code that will cause problems before I go to the server operators to ask why this is not working well.
Below is a very optimized version of the generator (which also has the same timeout problems)
protected void Page_Load(object sender, EventArgs e)
{
const int stageWidth = 500;
const int stageHeight = 200;
Bitmap createdImage = new Bitmap(stageWidth, stageHeight);
Graphics imageCanvas = Graphics.FromImage(createdImage);
if (!string.IsNullOrEmpty(Request.QueryString["t"]))
{
string imageText = Uri.UnescapeDataString(Request.QueryString["t"]).Trim();
Font font = new Font("Arial", 22);
imageCanvas.DrawString(imageText, font, Brushes.White, 0, 0);
}
MemoryStream memStream = new MemoryStream();
createdImage.Save(memStream, ImageFormat.Jpeg);
imageCanvas.Dispose();
createdImage.Dispose();
Response.AppendHeader("content-disposition", "filename=MyImage");
Response.ContentType = "image/jpeg";
memStream.WriteTo(Response.OutputStream);
memStream.Dispose();
Response.Flush();
}