True Image Resizing, Part 2
By Scott Mitchell
In Part 1 we examined how to create an ASP.NET Web page,
ShowImage.aspx, that would display an image. This ASP.NET Web page could then be referenced
via an HTML <img> tag. In this part we'll look at how to extend the ShowImage.aspx
so that it can produce thumbnail images; then, we'll see how to tie this lesson together with the lessons
learned from the article Displaying a List of Scaled Images.
Altering ShowImage.aspx to Create a Thumbnail
Earlier we saw that we could create an ASP.NET Web page called
ShowImage.aspx that could
be passed in an image file name and display the image. In addition to accepting the file name, let's
augment this ASP.NET Web page so that we can pass in an optional height and width. When a height and
width are passed in, the image displayed is first dynamically resized to the specified height and width.
The following code implements the needed changes in ShowImage.aspx.
|
Most of the code has remained the same. The important additions are two new variables, imageHeight
and imageWidth, which are assigned the QueryString values h and w.
An If statement is used to check if the height and width have been specified; if they have,
then the image is resized using the GetThumbnailImage() method call. If the height and
width have not been specified, then the image is outputted without any resizing.
| Resizing Images that Already Include Thumbnails |
|---|
Alert 4Guys readers Travis R. wrote in to say:
Just wanted to comment on your article in which you use the 4Guys enthusiast Doug P. chimes in with a workaround: I've found that if you rotate the original image 360 degrees it solves this problem. I guess rotating the image causes it to lose the embedded thumbnail and the full size image is then resized when the |
Putting It All Together: Using ShowImage.aspx to Display the Images in a Directory
Now that we have examined how to dynamically resize an image using the
GetThumbnailImage() method call, let's discuss how to integrate this with the
ASP.NET Web page that listed the images in a directory. (If you have not read the article
that this article is based on - Displaying a List of Scaled Images - you
should do so now.)
In Displaying a List of Scaled Images we saw an ASP.NET Web page
that used the Image class to determine the height and width of each image in a directory.
If the height or width were beyond certain bounds, the image's height and width attributes were scaled
via the height and width attributes of the <img> tag.
The code to do this was as follows:
|
Simple enough. Now, to use the dynamic resizing, rather than having the <img>
tag's src property refer to the actual image URL, we'll have it refer to ShowImage.aspx,
passing in the image's URL, height, and width through the QueryString. This will change the code that
sets the html variable to:
|
The reason there's an If statement is because we don't always need to resize the image.
That is, if the image is within the size constraints, then the image doesn't need to be resized.
In such a case, the QueryString for ShowImage.aspx does not include the w
and h parameters.
Conclusion
In this article we examined how to dynamically resize an image using the
GetThumbnailImage() method of the Image class. Using this method and
an ASP.NET Web page designed for displaying images, we retooled an earlier demo to allow for
the images to be dynamically resized, as opposed to just scaling the image through the <img>
tag's height and width attributes.
Happy Programming!




