Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Image Manipulation with ASP.NET 2.0


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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.