views:

68

answers:

1

I'm not good at putting up a question, feel free to reword/retag this to make it more understandable. Thanks.

I have an image proxy page helps me handle all requests to images of product with custom manipulation values. For example:

http://mysite.com/ImageProxy.aspx?id={ITEM ID}&width={WIDTH}&height={HEIGHT}&gloss={GLOSS}&reflection={REFLECTION}

To reduce server-side loads, I cache by saving the files of already generated images for reuse when the same product with same manipulation values are called.

This works at the beginning but is getting slower when more images shown in a single page. I want to know how I can improve this situation, or is image proxy tends to be slow by its natural?

*Edited: *

I am:

  • developing with ASP.NET MVC
  • using System.Drawing.Drawing2D, System.Drawing.Imaging to manipulate images.
  • Currently a page is about ~500kb in size *(of result page) with 8~10 images per page and 30~50kb (PNG) for each image.
  • image is currently PNG with transparency as reflection and rounded-corner effects as there as option and image may sit on various of background solid background color (ie/ GIF may be an option).
+1  A: 

Try this:

    // GetCacheFileName -> Returns the full path of the "cached" image
    // CreateImage -> Used to create a new image if necessary
    string cacheFile = GetCacheFileName(param1, param2, param3, param4);
    if (!File.Exists(cacheFile))
    {
        Image cacheImage = CreateImage(param1, param2, param3, param4);
        cacheImage.Save(cacheFile, ImageFormat.Jpeg);
    }

    Response.ContentType = "image/jpeg";
    Response.TransmitFile(cacheFile);

The two things to notice here are:

  • Tell the framework to save the image as jpeg
  • Save the file to disk and use Response.TransmitFile instead of Response.WriteFile or Response.OutputStream.Write
Badaro
due to need for transparency and opacity outputs are always PNG (eventhough the original file is JPG). Also can you elaborate reason to use `Response.TransmitFile` instead of `Response.OutputStream.Write` (what I'm currently using)? Thanks.
rockacola
I suggested JPEG just to minimize the size of the files, since one of the issues is the overall size of the page. The suggestion will still work with PNG as long as you use the proper content-type. And you should use Response.TransmitFile because it won't load the entire file in memory before transferring to the client, therefore being faster and far less resource-intensive.
Badaro