If you’ve ever attempted to create image thumbnails for your site, you’ll know it’s a tiresome task. You either do it manually, or use an inflexible system such as the FrontPage thumbnail feature.
However, as you’ve seen in previous tips, ASP.NET gives us tremendous control over how our images work. As such, we should be able to generate thumbnails on the fly… and this snippet will enable you to do just that.
Create a new Web form (thumbnail.aspx) and add the following code in listing 1 behind the page Load event. (Listing one is at the bottom of the article)
If you follow the code in Listing 1 code through, you should see what happens. The filename is retrieved from the query string and the image loaded into memory. The new width is then taken and a proportionate height calculated (an important usability feat that few demonstration code snippets take into account). Then, a newly sized thumbnail is generated and sent straight back down to the client.
After compiling, you’d call this page by typing something like the following URL into your browser: http://localhost/WebAppFolderName/thumbnail.aspx?filename=myfile.gif&width=100. You’d use this URL as the image source for, say, an Image control.
Don’t forget: after you’ve generated the thumbnail, you can still dynamically edit your image. Using the last tip, for example, you could add a little copyright notice next to each thumbnail. Powerful stuff, this imaging lark.
Figure: My company logo, resized to perfection
' Initialize objects Dim objImage, objThumbnail As System.Drawing.Image Dim strServerPath, strFilename As String Dim shtWidth, shtHeight As Short ' Get image folder path on server - use "" string if root strServerPath = Server.MapPath("WebAppImageFolder") ' Retrieve name of file to resize from query string strFilename = strServerPath & Request.QueryString("filename") ' Retrieve file, or error.gif if not available Try objImage = objImage.FromFile(strFilename) Catch objImage = objImage.FromFile(strServerPath & "error.gif") End Try ' Retrieve width from query string If Request.QueryString("width") = Nothing Then shtWidth = objImage.Width ElseIf Request.QueryString("width") < 1 Then shtWidth = 100 Else shtWidth = Request.QueryString("width") End If ' Work out a proportionate height from width shtHeight = objImage.Height / (objImage.Width / shtWidth) ' Create thumbnail objThumbnail = objImage.GetThumbnailImage(shtWidth, _ shtHeight, Nothing, System.IntPtr.Zero) ' Send down to client Response.ContentType = "image/jpeg" objThumbnail.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg) ' Tidy up objImage.Dispose() objThumbnail.Dispose()
About the Author
Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book (ISBN 1-59059-106-2), plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.
# # #