Generating ASP.NET Images on the Fly

This is an article discussion thread for discussing the SitePoint article, “Generating ASP.NET Images on the Fly

Thanks for the article. Both of these are extremely practical and will come in handy. I can’t believe how easy it is to create the thumbnails.

Looking forward to seeing more articles.

GD library for PHP are much easy to use and more fast than GDI!

i would like to see that statement backed up. Sounds like you are saying anything to degrade .net

I have a site which is hosted by a third party, and I am hoping to generate “text images” using font which is not installed on the server. Any ideas?!

i don’t see how you can do it on the “third party” server. the font would have to be installed to be used. you have 2 options in my opinion: 1) do the image generation on a different server with the font(s) and use a full url path to the image (or ftp it to third party server); 2) ask if they will install the font (but will probably be copyright issues their if the font requires a license to use like most do)

You can have a look at this solution:

there a font is used to creat a barcode - but every other font will do it as well…

What about a tutorial on how to create scaled-down images without using getThumnbnailImage? I often want to scale from 640x480 to, say, 320 x 240, and this method doesn’t provide the necessary quality. Can anyone help?

Here is something I wrote that may help you out:

 protected void GenerateImage(string istrImage, int iintHeight, int iintWidth)
 			int intHeight = (iintHeight != -1 ? iintHeight : Convert.ToInt32(ConfigurationSettings.AppSettings["strHeight"]) );
 			int intWidth = (iintWidth != -1 ? iintWidth  : Convert.ToInt32(ConfigurationSettings.AppSettings["strWidth"]) );
 			System.Drawing.Image imgFullSize;
 			imgFullSize = System.Drawing.Image.FromFile(istrImage);    		
 			this.Response.ContentType = "image/jpeg";
 			if(imgFullSize.Height > imgFullSize.Width)
 				int intTempWidth = intHeight;
 				int intTempHeight = intWidth;
 				intWidth = intTempHeight;
 				intHeight = intWidth;
 			Bitmap bpImage = new Bitmap(intWidth,intHeight);
 			Graphics gfxImage = Graphics.FromImage(bpImage);
 		    gfxImage.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
 			gfxImage.DrawImage(imgFullSize, 0, 0,intWidth, intHeight);
 		    bpImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

I only see the code example for “Create a Text Image on the Fly” in the source code for “Create a Thumbnail on the Fly” available, too? Thanks!

This was a very good begining tutorial, however, it is much more worthwhile to make this code dynamic (in turn this lends to portability) Using the above example is quick and clean but I find that hardcoding a dropdown box and then using a SELECT statement can be cumbersome. What would be better, in my opinion, is populate the dropdownlist with data from a datatable, the text would be the color name while the value could hold the colors HEX value. For example, RED #ff2400 then use ColorTranslator.FromHTML([Hex Value]) to define your color. Much eaiser I think.

Your image resizing program used to produce thumbnails should be written as an .ashx file instead of an .aspx file. Why? In the .ashx file, you only bring in the amount of system resources need to generate the thumbnail. Google .ashx for some more info on the subject.

Respect Sir, thats a great article :slight_smile:

Useful but I have to say, writing the file to disk and opening it isnt the best way.

You could set the response type to Image/Jpg and then output directly

Yes, an HTTPHandler or a Container page would be a much better approach which outputs a content-type.

Otherwise the IO operations become a bottleneck as the applications scales.


The only problem I have with saving to memory is that the gerneated image name on the web page all have the same filename.

in this case, they will all be


when you try to save them using “save as Image” in the browser, a huge hassel.

I have yet to figure out a way to change that.

I am using the same method for drawing some charts but the data i am using for drawing chart is too large. how shall i overcome this problem. It is taking too long to draw the image and as well as size can not be specified.

Don’t forget to use some constants in your code to limit the size of the images - you don’t want someone to request ‘thumbnail.aspx?image=myimg.jpg&w=10000000&h=10000000’, and neither does your server.

Just set some constants such as MAX_WIDTH = 150; MAX_HEIGHT = 150 and compare the values against these: if the querystring values are larger than the maximum values, discard them and use MAX_WIDTH and MAX_HEIGHT.

to change the filename, do this:

Response.AppendHeader(“content-disposition”, “attachment; filename=\“mycustomfile.jpg\””);

I am trying to send a PNG image directly to the output stream, but seems to work only with JPG or GIF formats, is there any limitation in this technique ?