Alex Mueller on Software and Technology 
Monday, December 20, 2004
I recently came across a situation in an application of mine where I needed to display images dynamically. I decided to display them in a table, creating rows dynamically as needed, and creating server controls dynamically as well. The images were user uploaded to the file system prior to this control loading. I was basically rendering the images and allowing control to them on the back end so a user could provide headings or captions for each image, as well as sort the image order, and delete.

I decided I did not feel like creating thumbnail images for each user uploaded image, because I did not want to deal with the overhead of maintaining them. In this case, I created a new ASPX page, named Imager.aspx, and basically its purpose is to render my thumbnail images to the output stream.

From my control where I dynamically create my images and their controls, I set my image ImageUrl or Src attribute to be “Imager.aspx” followed by any QueryString logic which may help me process the image. When my Imager.aspx page loads, I call the following code. This pretty much comprises my Imager.aspx.cs class.

private void Page_Load(object sender, System.EventArgs e) {
   
   
// This page is only used to make thumbnail images of a larger photo
   // without storing them on the file system. In the previous page, we
   // set the Image.ImageUrl = "Imager.aspx" and pass it some query string
   // params. We then manipulate the data, and crop the image, and display
   // the image to the Response.OutputStream.

   string fileName = Request.QueryString["mls"];
   string path = ConfigurationSettings.AppSettings["imagedirectory"] + "\\" + fileName.Substring(fileName.Length-1,1);

   // Load the file from the file system
   Bitmap bm = new Bitmap(Session["TheFile"].ToString());

   // Get the thumbnail image
   System.Drawing.Image img = bm.GetThumbnailImage(120,120,null,IntPtr.Zero);
   bm.Dispose();

   // Create a new bitmap to save to the output stream
   bm = new Bitmap(img);
   img.Dispose();
   bm.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
   bm.Dispose();   
   Session.Remove("TheFile");
  }

The only issue I ran into was the disposal of Images and Bitmaps. Without calling Dispose(), the apps fails when trying to access the image after this process of creating thumbnails occurs. Discovering this error while debugging an application on a live webserver can be a time consuming process. I think I'll use this thumbnail image logic more often.

Monday, December 20, 2004 8:44:29 AM (Mountain Standard Time, UTC-07:00) | Comments [0] | #
Comments are closed.
MuellerDesigns.net
Search
On This Page
PowerShell Documentation
Automate Daily Tasks with PowerShell
SketchPath XPath Editor
Software Testing - Revisited
Architecting Buildings and Software
NBCOlympics.com with Silverlight
Marker Interfaces and C# Attributes
The Phone Screen
Working with ASP.NET MVC and MvcContrib
Thanks to BDD
Twitter
The Opposite of a Singleton?
Removing Duplicate Code in Functions
Add Vista Themes to Longhorn
Changing File Ownership In Vista and Longhorn
Most Popular
JavaScript ReplaceAll Functionality
What is polymorphism?
What is composition?
Sorting with IComparable and IComparer
Applying the Observer Pattern in ASP.NET
MVP in ASP.NET
What is abstraction?
What is encapsulation?
What is a class?
What is inheritance?
Authentication in ASP.NET
Calendar Controls
XPathNavigator.CheckValidity new for 2.0
SQL Server 2005 Connection Issues
Auto-attach to process '[####] aspnet_wp.exe' on m...
What is an object?
FreeTextBox
VMWare and VPC
An Example of Reflection using C#
Caring for the Team
Archive
Links
Categories
My Local Blog Map
Blogroll
About
Powered by:

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2009
MuellerDesigns.net

Sign In

Help Those In Need
The Hunger Site
Ronald McDonald House Charities (RMHC) of Western Washington & Alaska