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.
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