Click here to Skip to main content
11,795,848 members (80,281 online)
Click here to Skip to main content

Thumbnail Image Creation and Image format Conversion Utility

, 5 Jun 2002 185.1K 2K 75
Rate this:
Please Sign up or sign in to vote.
This code will convert an original image to a thumbnail without writing to the hard disk. You also have the option to save and to specify width and height of the thumbnail image through the QueryString.
<!-- Article image -->

Sample Image - thumb.gif

<!-- Add the rest of your HTML here -->

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
            originalimg = originalimg.FromFile(FileName) ' Fetch User Filename
            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
            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
            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.

  End Sub



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.







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


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

Web Developer
India India
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.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 2 Pin
Saud AKhter9-Jun-09 0:12
memberSaud AKhter9-Jun-09 0:12 
GeneralAutomatic thumbnail generation Pin
zioturo2-Nov-05 5:09
memberzioturo2-Nov-05 5:09 
GeneralRe: Automatic thumbnail generation Pin
Alexandru Stanciu13-Jul-06 2:16
memberAlexandru Stanciu13-Jul-06 2:16 
GeneralResize works once, but not twice Pin
Anonymous19-Aug-05 5:08
sussAnonymous19-Aug-05 5:08 
QuestionQuality ? Pin
T.Pappas11-Nov-04 8:52
memberT.Pappas11-Nov-04 8:52 
GeneralNeed help - Access is denied Pin
benreily6-Jun-04 14:19
memberbenreily6-Jun-04 14:19 
QuestionWhat is wrong ? Pin
MasterII11-Feb-04 14:14
sussMasterII11-Feb-04 14:14 
AnswerRe: What is wrong ? Pin
Karthikg11-Feb-04 17:29
memberKarthikg11-Feb-04 17:29 
GeneralRe: What is wrong ? Pin
MasterII13-Feb-04 0:26
sussMasterII13-Feb-04 0:26 
GeneralRe: What is wrong ? Pin
Karthikg13-Feb-04 0:47
memberKarthikg13-Feb-04 0:47 
GeneralRe: What is wrong ? Pin
MasterII13-Feb-04 1:15
memberMasterII13-Feb-04 1:15 
GeneralSolution Pin
MasterII13-Feb-04 1:28
memberMasterII13-Feb-04 1:28 
Generalresize image upload before save Pin
K94U7-Jan-04 14:44
sussK94U7-Jan-04 14:44 
Generalaspect ratio Pin
loreille29-Nov-03 2:47
memberloreille29-Nov-03 2:47 
GeneralRe: aspect ratio Pin
unlisted11-Jan-04 11:39
memberunlisted11-Jan-04 11:39 
GeneralRe: aspect ratio Pin
cry4dawn12-Oct-05 11:17
susscry4dawn12-Oct-05 11:17 
GeneralZoom and thumbnails Pin
Rferj16-Mar-03 9:21
memberRferj16-Mar-03 9:21 
GeneralRe: Zoom and thumbnails Pin
Forum Admin29-Aug-03 5:17
sussForum Admin29-Aug-03 5:17 
QuestionHow do I save it to disk Pin
Classe11-Sep-02 2:01
memberClasse11-Sep-02 2:01 
AnswerRe: How do I save it to disk Pin
Steven Behnke20-Apr-03 14:03
memberSteven Behnke20-Apr-03 14:03 
GeneralRe: How do I save it to disk Pin
archy24-May-03 3:37
memberarchy24-May-03 3:37 
GeneralRe: How do I save it to disk Pin
eviler6-Jan-04 23:05
membereviler6-Jan-04 23:05 
GeneralRe: How do I save it to disk Pin
archy7-Jan-04 4:10
memberarchy7-Jan-04 4:10 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151002.1 | Last Updated 6 Jun 2002
Article Copyright 2002 by Karthikg
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid