Tuesday, June 22, 2010

Image Generator control in ASP.Net 3.5

Storing images in database BLOB field and displaying it on aspx page is one of the common tasks we do in asp.net projects. Asp.Net itself does not have an in build control to bind the image stored in database. To display an image stored in database, we will write an HttpHandler which will fetch the image from database and do a binary write. We all know that doing a binary write will become cumbersome when the number of images increases and the number of users becomes high. This week Microsoft have released a new control called ASP.NET Generated Image control to display image in ASP.Net page.

Download the control (Microsoft.Web.GeneratedImage.dll) from
http://aspnet.codeplex.com/releases/view/16449

This control can be used to display image faster, we can cache the generated image and we can do transformation on the generated image. To display the image, this control uses an Image HttpHandler which can accept parameters using NameValueCollection object. This Image HttpHandler is similar to normal HttpHandler which inherits an abstract class called ImageHandler. This ImageHandler abstract class internally inherits IHttpHandler.



public class ImageHandler1 : ImageHandler {



public ImageHandler1() {

// Set caching settings and add image transformations here

}



public override ImageInfo GenerateImage(NameValueCollection parameters) {

return new ImageInfo();

}

}

The implementation is really simple. Read the image from database as a byte array and convert the byte array to Microsoft.Web.ImageInfo object for the image to display. This object will come as a part of the control dll we are downloading. ImageInfo object has 2 overloaded constructors, one will take Image object and the other will take a byte array as an argument. We can pass the parameters to this handler by a collection called Parameters in GeneratedImage control. With this information, we will see how we can use this control to display images.

refere below article for more details...
http://www.codedigest.com/Articles/ASPNET/119_New_Image_Generator_control_in_ASPNet_35.aspx

No comments:

Post a Comment