Click here to Skip to main content
Click here to Skip to main content

Thumbnail Image Creation and Image format Conversion Utility

By , 5 Jun 2002
 

Sample Image - thumb.gif

Generally we were required, in our classic ASP applications, to generate thumbnail images by using a 3rd party COM component. To do the same in the .NET Framework, you need not depend any more on those tools. It has built in functionality which will convert an original image to a thumbnail without ever writing a physical file to the hard disk. Of course, you have the option to save the result if you want. It will allow users to specify the width and height of the thumbnail image using a QueryString. It handles exceptions by throwing an image not found, in case you give a bad filename or invalid parameters. This code can be written within an aspx page, there is no need to use code behind.

<script language="VB" runat="server">

  Sub Page_Load(Sender As Object, E As EventArgs)
	
        Dim originalimg, thumb As System.Drawing.Image
        Dim FileName As String
        Dim inp As New IntPtr()
        Dim width, height As Integer
        Dim rootpath As String

        rootpath = Server.MapPath("/") ' Get Root Application Folder

        FileName = rootpath & Request.QueryString("FileName") ' Root Folder + FileName
        Try
            originalimg = originalimg.FromFile(FileName) ' Fetch User Filename
        Catch
            originalimg = originalimg.FromFile(rootpath & "error.gif") ' Fetch error.gif
        End Try

        ' Get width using QueryString.
        If Request.QueryString("width") = Nothing Then
            width = originalimg.Width  ' Use original Width. 
        ElseIf Request.QueryString("width") = 0 Then  ' Assign default width of 100.
            width = 100
        Else
            width = Request.QueryString("width") ' Use User Specified width.
        End If

        ' Get height using QueryString.
        If Request.QueryString("height") = Nothing Then
            height = originalimg.Height ' Use original Height.
        ElseIf Request.QueryString("height") = 0 Then ' Assign default height of 100.
            height = 100
        Else
            height = Request.QueryString("height") ' Use User Specified height.
        End If

        thumb = originalimg.GetThumbnailImage(width, height, Nothing, inp)

        ' Sending Response JPEG type to the browser. 
        Response.ContentType = "image/jpeg"
        thumb.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)

        ' Disposing the objects.
        originalimg.Dispose()
        thumb.Dispose()

  End Sub
</script>

Usage

http://<servername>/thumbnailimage.aspx?filename=<filename>&width=75&height=75

Where filename should be path relative to the root of the application folder. width & height are optional. If not specified, then the original width and height will be used.

If width or height are specified as 0, then the default width / height of 100 pixels is used.

Examples

http://localhost/thumbnailimage.aspx?filename=win2000.gif

http://localhost/thumbnailimage.aspx?filename=win2000.gif&width=0&height=0

http://localhost/thumbnailimage.aspx?filename=win2000.gif&width=50&height=50

http://localhost/test/thumbnailimage.aspx?filename=test/win2000.gif

Notes

By default the thumbnail image generated is of type JPEG. One can change the Response.Type by changing the following code.

Response.ContentType = "image/jpeg"
thumb.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg) 'Change jpeg word 

with the required supported format(Gif, Png, Bmp etc).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Karthikg
Web Developer
India India
Member
I am working as Technical Lead at Virtusa, Hyderabad, India. I have more than 6 yrs of experience on the Microsoft Web Platform. I have been working in and out DotNet Technology since Pre Beta Release. The articles are the outcome of the R&D I do in my freetime.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 2memberSaud AKhter9 Jun '09 - 0:12 
Not at all handy stuff
GeneralAutomatic thumbnail generationmemberzioturo2 Nov '05 - 5:09 
The easiest way to upload and resize an image to the internet is I-Load.
This component also create an unlimited number of thumbnails from the original image with the required size.
I-Load is a FREE ASP.NET web control with numerous benefits and features.
You can download I-Load (it's FREE!) and view an online demo here:
 
http://www.radactive.com/en/Products/ILoad/Overview.aspx
GeneralRe: Automatic thumbnail generationmemberAlexandru Stanciu13 Jul '06 - 2:16 
It seems it's not free anymore. Frown | :(
GeneralResize works once, but not twicesussAnonymous19 Aug '05 - 5:08 
I am successful in using this script to create thumbnails (125 pixels wide) of large images (say originals are 1000 pixels wide). I want then to offer users a chance to enlarge to say 500 pixels wide. My problem is that when the 500 pixel wide version opens up in its own, new window, it is simply a stretched version of the 125 pixel wide thumb on the calling page, not the original 1000 pixel wide version on the server. Any ideas? How can I tell the server to use the original, not the thumbnail, in the second resize request?
QuestionQuality ?memberT.Pappas11 Nov '04 - 8:52 
Thanks a lot for this code.
 
Is it possible to have a thumb-on-the-fly with quality adjusting ?
( ex.: width=200,height=200,quality=50% )

GeneralNeed help - Access is deniedmemberbenreily6 Jun '04 - 14:19 
Hi, I am still new to ASP.net and especially poor at a configuration. I download the thumb.zip and extract the files into the wwwroot directory. I launch the script from my IE but it gives me this error.
 
http://localhost/alphacar/thumbnailimage.aspx?filename=images/book.jpg&width=75&height=75
 
Please help me and thanks in advance!
 
benreily
 
Server Error in '/alphacar' Application.
-----------------------------------------------------------------------------
 
Access is denied.
Description: An error occurred while accessing the resources required to serve this request. You might not have permission to view the requested resources.
 
Error message 401.3: You do not have permission to view this directory or page using the credentials you supplied (access denied due to ACLs). Ask the Web server's administrator to give you access to 'c:\inetpub\wwwroot\alphacar\thumbnailimage.aspx'.

QuestionWhat is wrong ?sussMasterII11 Feb '04 - 14:14 
I put the code exactly to aspx page, running on Win2003 server, but always get this error:
 
Compiler Error Message: BC30451: Name 'Imaging' is not declared.
 
Source Error:
 

 
Line 39: ' Sending Response JPEG type to the browser.
Line 40: Response.ContentType = "image/jpeg"
Line 41: thumb.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
Line 42:
Line 43: ' Disposing the objects.


AnswerRe: What is wrong ?memberKarthikg11 Feb '04 - 17:29 
Pls add
System.Drawing namespace.
It should work then.
 
Thanks,
Karthik
GeneralRe: What is wrong ?sussMasterII13 Feb '04 - 0:26 
I add
thumb.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
and :
 
Compiler Error Message: BC30182: Type expected.
Source Error:
 
Line 2:
Line 3: Sub Page_Load(Sender As Object, E As EventArgs)
Line 4: Dim originalimg, thumb As System.Drawing.Imaging
Line 5: Dim FileName As String
Line 6: Dim inp As New IntPtr()

Source File: Line: 4
 
WTF | :WTF:
GeneralRe: What is wrong ?memberKarthikg13 Feb '04 - 0:47 
Try this in Line 4.
 
Dim originalimg As System.Drawing.Imaging
Dim thumb As System.Drawing.Imaging
 
Hope this helps.
 
Karthik

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 6 Jun 2002
Article Copyright 2002 by Karthikg
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid