FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Dobbs M-Dev
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
February 02, 2007

Image Manipulation with ASP.NET 2.0

(Page 2 of 4)

Http Handlers

GraphicsDLL provides all the necessary bitmap manipulations, but how does the web site serve up images in response to HTTP requests? It uses an Http Handler. Http Handlers are classes that implement the IHttpHandler interface to return content to service HTTP requests. If you've programmed ASP.NET applications before, you've used at least one Http Handler—the ASP.NET Page class is an Http Handler that serves up HTML content.

Any class that implements the IHttpHandler interface must have a ProcessRequest method and an IsReusable property. The ProcessRequest method services requests and returns the requested content, along with an HTTP status code. The sample web site's ImageHandler class (Listing Two, Imagehandler.cs, available electronically) is an Http Handler that returns bitmaps manipulated by the GraphicsDLL assembly. The ProcessRequest method first calls context.Response.Clear to clear any HTTP headers and content that might be attached to the response. Then it specifies that it returns content in "image/jpeg" format. ProcessRequest returns a status code of 200 if the bitmap request is successful.

It's rude for a web site to link to images hosted on your web site without your permission, because those links consume your server's bandwidth. But this problem is easy to avoid. The InvalidImageLink method returns True if someone else's web site is linking to your site's images. When InvalidImageLink returns True, ProcessRequest returns a status code of 403 (forbidden). If the link isn't invalid, ProcessRequest calls ProcessBitmap to serve up the requested image.

ProcessBitmap first instantiates an ImgTagInfo object based on the request's URL and query string values. Next, an SHA-1 hash is computed from the ImgTagInfo properties, which correspond to the URL's query string values. If this hash doesn't match the request's hash value, the original query string parameters were changed. In this case, no image is returned, and an HTTP status code of 403 is returned. If ProcessBitmap determines that the query string parameters were not changed, it returns a status code of 200 (success) unless the bitmap file can't be found. The context.Response.Cache.SetExpires call specifies that the image should be cached on the client computer for 60 minutes. Caching images on the user's machine dramatically improves your web site's performance and user experience, since a given image is not repeatedly downloaded as users navigate through your web site.

If ImgTagInfo's Cache property is True, the image may be in the ASP.NET in-memory cache. If the bitmap isn't found in the cache, RenderBitmap is called to create the bitmap with the requested formatting options. The Bitmap.Save call sends the bitmap to the user by writing it to the response output stream. If the Cache property is True, and the image isn't already in the cache, AddBitmapToCache is called to cache the image. Since the image is an EnhBitmap object, which implements IDisposable, AddBitmapToCache uses a CacheItemRemovedCallback delegate to ensure that the object is properly disposed the moment it's removed from the cache. If the bitmap is not cached, its Dispose method is called.

Previous Page | 1 Image Manipulation with ASP.NET 2.0 | 2 Http Handlers | 3 Configuring Http Handlers | 4 Rendering Bitmaps Next Page
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK