views:

350

answers:

6

I am wondering which way is the fastest to deliver images via ASP.net:

//get file path
string filepath = GetFilePath();
Response.TransmitFile(f);

or:

string filepath = GetFilePath();
context.Response.WriteFile(f);

or

Bitmap bmp = GetBitmap()
bmp.Save(Response.OutputStream);

or any other method you can think of

A: 

It's easy enough to test, I recommend you set up three different URLs that will test the different mechanisms and then have a client (HttpWebRequest/HttpWebResponse or WebClient instance) download the content from all of them. Use a Stopwatch instance to time the download.

I imagine that it's not going to matter, that network latency is going to trump IO latency (unless you are thrashing the hard drive all the time) most of the time.

casperOne
+1  A: 

This doesn’t really answer your question but asp is not a file server, if you want to serve files use IIS and get Asp to link to those files, or if you must use ASP use it to redirect to the appropriate place.

I am not saying that it can't be done but if you are worried about performance, you may consider going down another route.

Of the methods you have I would think that the bitmap one would be slowest as that is creating a more complex object.

MS seems to have a decent solution if you must do it through asp.

Jeremy French
A: 

Well one thing is for sure - this will NOT be as fast as letting IIS server the file. If route the request through asp.net instead of letting IIS serve it then you are introducing a bunch of overhead you dont need.

The only reason I can imagine routing through asp.net is for security purposes; is that the case?

keithwarren7
A: 

In my experience, use TransmitFile(), as long as that's the only thing you intend to send, and it sounds like it is.

Note this is incompatible with AJAX-enabled ASPX files.

tsilb
+3  A: 

TransmitFile scales better since it does not load the file into Application memory.

You'll need to test with large image files to see a visible difference, but TransmitFile will outputperform WriteFile.

In either case, you should use an ashx handler rather than an aspx page to serve the image. aspx has extra overhead which is not needed.

One more thing-- set the ContentType when sending the file or the browser may render it as binary gibberish. In the case of BMP:

context.Response.ContentType="image/bmp";

frankadelic
Your link appears to have died, can you update it?
JohnFx
Done... linked to forum discussion.
frankadelic
+1 for the info in the link
Michel
A: 

I imagine you want to do this either out of security concerns, or to record some type of metrics (e.g. recording each hit to the database to find out what image is most popular, or who is viewing the image, etc.), or for URL rewriting purposes. If there is no particular reason to use ASP.NET to serve the image, then you should just let IIS take care of it as others have noted.

Also - this doesn't answer your question of which method is most efficient when reading an image file from disk, but I thought I should point this out:

If you already have a Stream or Bitmap containing the image, use that to write directly to Response.OutputStream. You definitely want to avoid writing it to disk and then reading from disk if you already have the Stream.

Terrapin