There's something out there in life that will delight and amaze you.
Get up, get moving, and go find out what it is. - (Ralph Marston)

Saturday, June 07, 2008

Dynamically Generating Images using ASP.NET Handlers


Dynamic image generation in web applications are expensive tasks. More often we tend to access images from a location in a web server, or the regularly accessed images are often stored in cache objects. In some cases we need to draw images dynamically.

In any of these cases, more the resource are consumed the performance of the application reduces. There fore it is vital to use techniques that will improve the application performance. This article introduces the HttpHander and how it can be used to dynamically generate images providing a better performance to the application.

The HttpHander

It’s all to do with how the IIS handles a request. When the web application receives a request, it checks with its ISAPI filter. Internet Server Application Program Interface (ASPNET_ISAPI.DLL). This is responsible for getting the control on http requests. So when we have configured the application to handle the image types we require it will process them.

In this example jpeg is used. The configuration in the IIS will be explained later in this article.

Common Class to Handle Images

We need a class that will basically handle the images in our application. First we need to make this class, process the request that we get from the web page.

There fore, let us create a class that implements IhttpHandler. From the IhttpHandler implement the default members namely, the IsReusable and ProcessRequest. IsReusable method is responsible of indicating whether this particular instance can be used by another request or not, so here we will specify return false, as to indicate that it will be used by a single request. ProcessRequest is basically responsible of processing the http web request. So in this example I will transmit a particular person’s image based on the person’s name.


Utilizing the Common Class

Let us create a web page with a RadioButtonList control specifying the values (a,b,c) and text (person names) and a Panel control to load the images. Our objective is to display the person's image for selecting the name from the RadioButtonList control. And for this let us use the created class above.

Image control is created and the required ImageUrl is constructed and assigned to the ImageUrl property of the Image control before adding the control to the panel. Here, image URL is pointed to Test.jpg image, which actually does not exists in the solution. 

So, how does this actually work? Let us dive deep to this and see what happens inside. When passing the selected value as the query string to the Test.jpg, the IIS identifies that it is an image to be processed of type jpeg and then the ImageHandler class will response with the relevant image.


Setting the Web.config

For our application to inform that .jpg file types are handled by the HttpHandlers, a tag is added to the <httpHandlers> section specifying the path as *.jpg and verb as * to handle any jpg request and by using the class ImageHandler which is specified in the type.


Configuring IIS

As we mentioned earlier, to make IIS identify the images, an application mapping for the .jpg (The file Type)  that is handled by HttpHander needs to be configured in IIS. Follow are the steps for configuration.

· Open the IIS.

· Right Click on the Web Site.

· Properties -> Configuration -> Application Configuration-> Mapping

· Click Add

· Browse the aspnet_isapi .dll, type the extension as .jpg,

. Click on “Check that file exists” option.

Note (Only for Windows XP Professional Edition users) : In a Windows XP Professional Edition,  OK button is disabled in this configuration. To overcome this, Once you select the dll, just bring your cursor to that field and make it focused. You must click on the Executable field and you must click on the Detention fields. And then OK will be enabled.

After configuring the application let us run at application. And here is how it appears.

2 3

Performance Evaluation 

The following performance report depicts the most called functions and functions with more work.



Loading images dynamically using the handlers are much faster and less memory consuming compared to other techniques. Further, from a coding perspective it reduces the lines of code and keep things more clean and neat, from a performance perspective it improving the overall web application performance.



At 3:42 PM, Blogger RRave said...

Hey, second pic is my girl friend ya, how can you take that foto :)

At 10:40 PM, Blogger Gayani Devapriya said...

Oh Ravii..come on

At 9:17 PM, Blogger db-Consultants said...

Hi Gayani! I am enjoying your blog entries and also your contributions to Simple Talk.

I'm new to C#/; may I ask where you found the Performance Report Summary that is below the images in Dynamically generating images? Is it part of IIS?

Thanks! Jim

At 11:12 PM, Blogger Gayani Devapriya said...


Thank you very much for your comments. Glad you enjoy them all.

For the Performace Report you see at the last part of the post was generated by Visual Studio 2008. It’s not a part of IIS.

Here is how you could get a report for your web application.

1.First you need to have some application up and running in order to check the Performace Report.

2.In the Visual Studio main menu you can find an option called ‘Analyze’, click it and click Profiler->New Performance Session.

3.Then you will see a section Performace1 and under that 2 folders Targets and Reports in the Performance Explore. Now click on Launch Performace Wizard to specify your solution details and click ok.

4.And then click on Launch with Profiling option. This will take few minutes. You will see its status in the output window. And your application will run and once you close it you may see a file under the report section in the Performace Explore with a .vsp extention and it will automatically open once the process is complete.

Hope it helps.


Post a Comment

<< Home