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


Rendering Bitmaps

On-the-fly image manipulation is easy but has some significant performance costs. If you intend to use this technique on your web site, be sure to test the performance under load to determine if you can achieve your performance goals. Caching images in the ASP.NET cache may improve performance, but only if a small percentage of images and formatting options are requested a large percentage of the time. If your web site doesn't require dynamic image manipulation, you'll achieve much better performance by rendering your images in advance, and deploying the image files to your web site.

The web site project includes the console application PreRenderBitmap. Run this program in a Command prompt. If you run PreRenderBitmap without parameters, it displays a list of the parameters that it expects. The program has parameters for all of the formatting options that the ImageHandler accepts. PreRenderBitmap renders the image with the specified formatting, and then writes it to a .jpg file. If you use PreRenderBitmap to create all the bitmaps that your site displays, there's no need to use ImageHandler. Instead, you can rely on IIS to serve up images, or use the PreRenderedImageHandler. The PreRenderedImageHandler just checks to ensure that an image request is coming from your web site before it serves up the image. You'll need to change web.config to disable ImageHandler and enable PreRenderedImageHandler.

If your web site is hosted on a server that's shared by multiple companies, you may not have access to the IIS configuration to configure mappings for Http Handlers. If you're in this situation, you can use Generic Http Handlers, which require no IIS and no web.config configuration. To add a Generic Handler to your project, right-click the project, select Add New Item, and choose Generic Handler. Unlike Http Handlers that serve up content based on a file type, Generic Handlers are accessed by directly specifying them in URLs. For example, if you create a Generic Handler named "Handler.ashx," refer to it in a URL:


<img src="Handler.ashx?filename= 
   Images/2004_03_06_17_51_46.jpg?q=
      100&sx=0.5&sy=0.5 ..." />


The .NET Bitmap class plus Http Handlers make on-the-fly image generation simple. While caching images in the ASP.NET cache may improve performance, dynamic bitmap rendering has a high performance cost. Unless your web site requires dynamic image generation, you'll achieve optimal performance by rendering all images in advance and copying the image files to your web site. If you use an Http Handler to serve up images, remember to call Response.Cache.SetExpires to ensure that the image is cached on the client machine. After I set up my stock photography web site, you might start seeing my pictures in annoying advertisements littered all over your favorite web sites. I really hope so!


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.