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.
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.
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.
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.