6

I used .ashx handler for getting images from database.I want to retrieve a lot of images (>1000) in this way:

    <img src='GetImage.ashx?id= <%# Eval("id") %>'/>

(why I do this you can understand if read my previous question: bind database image to ItemTemplate in .ascx ).I am afraid that multipiles database querys (first query to get all id's,all others for getting image one by one) will take a lot of time,is it? What are possible solutions?

Community
  • 1
  • 1
Anton Putov
  • 1,951
  • 8
  • 34
  • 62
  • 1
    Do you have try it in real time ? from the moment you have decide to use ashx and not a file on disk, then you need to move that way, make it see if the time is not good first, then make optimizations. Browsers did not ask many images together, so probably is not take "a lot" of time. You can optimize the way you get the image on ashx later. – Aristos Oct 24 '12 at 06:39
  • 1
    I tryed it in real time (i find script that helps to insert a lot of images in database in on step: http://blog.naver.com/PostView.nhn?blogId=mcpop77&logNo=150025568733&beginTime=0&jumpingVid=&from=search&redirect=Log&widgetTypeCall=true ).My end answer is YES.I query 1000 images (24*24) and browser displayed it in 2 seconds.I think it is fast.but I steel not understand one little thing:why it is so fast?I dont beleive that even simple query takes < 0.002 seconds. – Anton Putov Oct 25 '12 at 11:07

2 Answers2

3

First of all the browsers did not ask the images all together, but few at a time.

Second, the handler is not use session, so its not lock the one the other and so a parallel process can be done for the image call.

The extra that I suggest to add it a cache for the browser, so when its load an image to not ask it again.

An example:

context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(120));
context.Response.Cache.SetMaxAge(new TimeSpan(0, 120, 0));

but you can add more aggressive cache.

One similar question: call aspx page to return an image randomly slow

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
3

Caching is always a good idea for static'ish content that is asked for frequently.

Also if the images are relatively small you could use data uri's http://css-tricks.com/data-uris/

Say you had an Images table with ID INT, Name VARCHAR(64), MimeType VARCHAR(64), and Data VARBINARY(MAX)

SELECT Name, 'data:' + MimeType + ';base64,' + cast(N'' as xml).value('xs:base64Binary(xs:hexBinary(sql:column("Data")))', 'VARCHAR(MAX)') AS DataURI

<img src='<%# Eval("DataURI") %>' alt='<%# Eval("Name") %> />

In this way you can have 1 database query that will return everything.

Louis Ricci
  • 20,804
  • 5
  • 48
  • 62