Tuesday, November 28, 2006

Dynamic image resizing and caching in ASP .NET

Recently I have faced a problem of caching in ASP .NET. What happened was, I was using an ASPX page to resize the image size by passing the required image width and image path in querystring. An example image resizing tag that I was using will look like this..


< i m g src="thumbnail.aspx?filename=" width="170" />

The above tag works perfectly when it was running on .NET framework 1.1 but when I changed my server to framework 2.0 all the images displayed are the same, no matter the image path and file name was changed!! I knew it was a problem with the caching feature in .NET 2.0. So I googled for a solution for this, and figured out this cool tag to control the caching based on the parameters passed to the ASPX page. Here’s the tag that you need to place at the top of the ASPX page.

< % @ OutputCache duration="1000" varybyparam="width; filename" %>

After implementing the above code the page rendered as expected, and the caching was controlled based on the width and file name passed in querystring. You can find more information about this and the serverside thumbnail script from here http://west-wind.com/weblog/posts/283.aspx

1 comment:

Anonymous said...

I developed a resizing module that you might want to check out... It's a teeny bit easier to use (You just add a query string to existing image URLs). As a side effect, it integrates nicely with the URL authorization system and is secure.

It also uses a managed disk cache for scalability. If you have a lot of images it becomes very important... You don't want to have to re-generate them every time the app recycles.


http://nathanaeljones.com/products/asp-net-image-resizer/