Recently, I've been wondering what the difference is between rendering an image using IIS's built in file handling capabilities, or using the asp.net with a custom handler. I decided to do a little test program to simply see the difference in clock time for processing such requests. The results of this test clearly show that IIS is faster. Sometimes a little, and sometimes a lot. I decided to test three size images, and run them each 10 times. Often, there is a start up lag time so the iterations should wash that out.
The table below basically sums up the results pretty well. The resolution of my timer is not great so for small images (35KB), both IIS and the handler processed the image in under 15 milliseconds. There may be a difference but in my case, it was probably not measurable because of all the other time involved in the request. For Medium size images (550KB), difference is more consistent. Again, it's a timer resolution problem that makes 15 milliseconds probably something related to a CPU click of sorts. For large images (5.6MB) you can clearly see a difference. The time it takes for the handler to process the image is almost double that of IIS.
Just for the fun of it, if you want to run a URL that has the program that generated these numbers, you can by going to the URL: http://livedemos.peterkellner.net/ASPNETIISTiming.aspx and the numbers will recalculate live. A couple things to keep in mind here. First, the above URL is running on a hosted site with lots of other customers (specifically, it's hosted at http://ultimahosts.net/ whom I recommend highly and it's running with IIS 6.0.
If you want to experiment yourself with this, I've listed at the bottom of this article the class I associate with an ObjectDataSource to generate these numbers (as well as the HttpHandler and the aspx page). The tables are each GridView's with incoming parameters to the ODS class of number of iterations and the image name in the /Images directory to process. It's really very straight forward.
The conclusion is pretty obvious for large images. That is, IIS is better. There are times when you must use a handler such as when you want to watermark your images or do other custom rendering. If, however that is not necessary, better to rely on IIS to process the images. Something also to keep in mind is that if you use the Wild Card Forwarding Option in IIS (only available in IIS6, not IIS5.1), the DefaultHttpHandler will automatically get invoked for you on file types you do not specifically handle and those images will bounce back and get processed by IIS 6.0. Here is a good reference from Dominick Baier describing how that works.
(GetWebData.cs) This is the class the ObjectDataSource References.
/// Summary description for GetWebData
[DataObject(true)] // This attribute allows the ObjectDataSource wizard to see this class
/// This is the Select Method used with the class.
/// filename should be the name of the file that is in the images directory