Demo of ShowImage.aspx with Thumbnail Support

This demo illustrates using the ASP.NET Web page ShowImage.aspx with thumbnail support. The following HTML is used in the demo:

<img src="/demos/ShowImage.aspx?img=dgeditss.gif" />
<img src="/demos/ShowImage.aspx?img=dgeditss.gif&w=100&h=100" />

If you right click on the two images and select to view the image properties, you can see that the first image, which is not resized since no height or width information is passed in, is over 16 KB. The thumbnail image, which has dynamnically been reduced to 100x100, is under 5 KB.

Source Code
Below is the source code for ShowImage.aspx with thumbnail support

<%@Import Namespace="System.Drawing.Imaging" %> <script language="VB" runat="server"> Function ThumbnailCallback() as Boolean Return False End Function Sub Page_Load(sender as Object, e as EventArgs) 'Read in the image filename to create a thumbnail of Dim imageUrl as String = Request.QueryString("img") 'Read in the width and height Dim imageHeight as Integer = Request.QueryString("h") Dim imageWidth as Integer = Request.QueryString("w") 'Make sure that the image URL doesn't contain any /'s or \'s If imageUrl.IndexOf("/") >= 0 Or imageUrl.IndexOf("\") >= 0 then 'We found a / or \ Response.End() End If 'Add on the appropriate directory imageUrl = "/images/" & imageUrl Dim fullSizeImg as System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(imageUrl)) 'Do we need to create a thumbnail? Response.ContentType = "image/gif" If imageHeight > 0 and imageWidth > 0 then Dim dummyCallBack as System.Drawing.Image.GetThumbNailImageAbort dummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback) Dim thumbNailImg as System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(imageWidth, imageHeight, dummyCallBack, IntPtr.Zero) thumbNailImg.Save(Response.OutputStream, ImageFormat.Gif) 'Clean up / Dispose... ThumbnailImg.Dispose() Else fullSizeImg.Save(Response.OutputStream, ImageFormat.Gif) End If 'Clean up / Dispose... fullSizeImg.Dispose() End Sub </script>

[Return to the article]