Click here to Skip to main content
12,076,206 members (40,979 online)
Click here to Skip to main content
Add your own
alternative version


99 bookmarked

An ASP Thumbnail Solution

, 19 Nov 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
ASP pages and free COM objects for generation of thumbnails and thumbnail views.

Sample Image - ThumbTools2-1.jpg

Important: Version 2 compiled with CxImage 6.00 - Supports all file types.


Some months ago, I wrote an article about a C# class that generates thumbnails and a reusable ASP.NET user control that displays thumbnail views. Several people asked me to port my solution to classic ASP. I found some time to do it and here I present the results. The ASP solution is functionally equivalent to that of ASP.NET (in fact, many pieces of code have been translated from C# to VBScript). Only some advanced options are missing (comments showing and editing, beveled thumbnails, thumbnail caching, thumbnail saving) because they are difficult to be implemented in classic ASP. The solution for thumbnail views is reusable and easy to use. You only have to make a virtual include and call a server function inside a form. To generate the thumbnail data in JPEG format, I created an ATL-COM object that is a wrapper of the CxImage class of Davide Pizzolato. CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, and J2K images. The COM wrapper object could be useful for other tasks also. For example, you could create a Web page that lets users apply image processing algorithms on their images.

In this article, I will describe how to use the thumbnail tools and also the important parts of the code.

How to Create a Single Thumbnail and Thumbnail Views

The ASP part of the thumbnail solution consists of the files ThumbGenerate.asp and After downloading the zip and extracting the files, make the containing ThumbAsp folder a virtual directory and point the browser to the TestListThumbs.asp page. This is a test page, an instance of which is shown in the figure above. Enter in the Virtual Path textbox a path of an IIS virtual-directory containing images, and press the 'Submit' button. You should see the thumbnails of the images residing in that directory. Try the other choices also and see the changes on the thumbnails.

ThumbGenerate.asp is a VBScript code file that can be used in an <img> tag to produce a thumbnail dynamically in JPEG format. A typical use of the ASP file is shown below. The <a> link points to an image and the <img> tag specifies a dynamically generated thumbnail of the same image via its src attribute value.

<a href=''>
<img src='
        border=0 alt='Name: SANTO007.jpg'/></a>

Parameters to the ASP file are given in the form of a query string. They are the following:

  • VFilePath: Virtual path of the original image. The only mandatory parameter. If the file specified does not exist or is not an image, a "no thumbnail" default image is generated from the NoThumb.gif file.
  • Width, Height: Desired thumbnail size. If not given, it is read from appSettings; if not specified in appSettings, defaults to 150x150.
  • Quality: JPEG quality parameter. Ranges from 1 (lowest quality) to 99 (highest quality). Defaults to 75.
  • AllowStretch: Set to 'True' to allow stretching of thumbnail to fit the above size. If not given, defaults to False.
  • ShellThumbnails: If not specified or set to 'False', the thumbnail is generated using the CxImage wrapper object implemented in CxImageATL.dll (ProgID='CxImageATL.CxImage'). If set to 'True', the thumbnail is generated using the COM object implemented in ThumbExtract.dll (ProgID='ThumbExtract.FileThumbExtract'). The latter object exploits the shell interface (IExtractImage) responsible for generating thumbnails when you click a file or select the Thumbnail view in Explorer. This allows you to create thumbnail views for various file types, e.g., PowerPoint presentations, like in the following figure.
  • See my article Create Thumbnail Extractor objects for your MFC document types about how to develop a COM object that can extract thumbnails for any file type by implementing the IExtractImage interface. In the same article, you can find the code for ThumbExtract.dll.

PPT-file thumbnails using the shell-thumbnail extract object is an ASP file that creates thumbnail views. To use it in an ASP page, you have to include the file as follows:

<!-- #include virtual ="/ThumbAsp/" -->

In the position you want the thumbnail view, you call the ListThumbs function that has the following syntax:

           Quality, AllowStretch,ShowFilenames,ShellThumbnails)

The parameters of the ListThumbs function have the following meaning:

  • VPath: Virtual directory that you want to display its contents as thumbnails.
  • Columns: Number of columns of the view.
  • Filter: Filter to select the files for which you want to display thumbnails (e.g., *.jpg;*.gif).
  • AllowPaging, PageSize: Set AllowPaging=True to show pages containing PageSize thumbnails each. For paging to work, you must call the function inside the first form of the ASP document.
  • Width, Height, AllowStretch, Quality, ShellThumbnails: See explanations above.
  • ShowFilenames: Set ShowFilenames=True to show the corresponding filename under each thumbnail. and ThumbGenerate.asp must be contained in the same virtual directory. Of course, you must register the COM objects also.

Source Code Explanations

As mentioned above, the CxImageATL object is a wrapper around the CxImage imaging class. This class has functions to load and save in all popular image formats and also various image processing and manipulation functions. For the thumbnail generation, the Resample function is used. I added a new function called ImageForASP that returns the image as a Variant byte array using any supported image format. The returned variant ImageData can be used to display the image from an ASP page by calling Response.BinaryWrite ImageData. The ImageType parameter defaults to 2 (JPEG), and the Quality parameter defaults to 75 and is used only for the JPEG format.

// method to display image in ASP pages
STDMETHODIMP CCxImage::ImageForASP(long ImageType, long Quality,
                                   VARIANT *ImageData)
    SAFEARRAY *psaData; BYTE *pData,*pBuffer; long size;
    SAFEARRAYBOUND rgsabound[1];
    CxMemFile memfile; memfile.Open();
    bool bOk = m_image.Encode(&memfile, ImageType);
    if(!bOk) return E_FAIL; // NO-IMAGE
    // create safe array and copy image data
    rgsabound[0].lLbound = 0; rgsabound[0].cElements = size;
    psaData = SafeArrayCreate(VT_UI1, 1, rgsabound);
    SafeArrayAccessData(psaData, (void **)&pData);
    // put data in variant
    ImageData->vt = (VT_ARRAY | VT_UI1);
    ImageData->parray = psaData;
    return S_OK;

ThumbExtract.dll has an equivalent function called ThumbJpgData that returns thumbnail image data in JPEG format (CxImage files were used again). Below is the code of the ThumbGenerate.asp file. The only tricky part was how to retain the aspect ratio of the original image. Notice also that the bit depth of the original image must be increased to 24 bits since JPEG does not support indexed images.

Set fso = Server.CreateObject("Scripting.FileSystemObject")
Response.ContentType = "image/jpeg"
' ---- parameter parsing ----'
Dim VFilePath,FilePath,Width,Height,Quality,bShellThumbnails,
VFilePath = Request("VFilePath")
If Len(VFilePath)=0 Then VFilePath="NoThumb.gif"
FilePath = Server.MapPath(VFilePath)
If Not fso.FileExists(FilePath) Then
  FilePath = Server.MapPath("NoThumb.gif")
End If
If Len(Request("Width"))>0 Then
  Width = Int(Request("Width"))
Else Width = 200
End If
If Len(Request("Height"))>0 Then
  Height = Int(Request("Height"))
Else Height = 200
End If
If Len(Request("Quality"))>0 Then
  Quality = Int(Request("Quality"))
Else Quality = 75
End If
If Len(Request("ShellThumbnails"))>0 Then
  bShellThumbnails = (Request("ShellThumbnails")="True")
Else bShellThumbnails = False
End If
If Len(Request("AllowStretch"))>0 Then
  bStretch = (Request("AllowStretch")="True")
Else bStretch = False
End If
Dim BinData
If bShellThumbnails Then
 ' Create COM Shell Thumbnail object
  Set objThumb = Server.CreateObject("ThumbExtract.FileThumbExtract")
  Call objThumb.SetPath(FilePath)
  Call objThumb.SetThumbnailSize(Width,Height)
  Call objThumb.ExtractThumbnail
  BinData = objThumb.ThumbJpgData(Quality)
 ' Create COM CxImage wrapper object
  Set objCxImage = Server.CreateObject("CxImageATL.CxImage")
  Call objCxImage.Load(FilePath,GetFileType(FilePath))
  Call objCxImage.IncreaseBpp(24)
  ' determine thumbnail size and resample original image data
  If Not bStretch Then ' retain aspect ratio
     widthOrig = CDbl(objCxImage.GetWidth())
     heightOrig = CDbl(objCxImage.GetHeight())
     fx = widthOrig/Width
     fy = heightOrig/Height 'subsample factors
     ' must fit in thumbnail size
     If fx>fy Then f=fx Else f=fy  ' Max(fx,fy)
     If f<1 Then f=1
     widthTh = Int(widthOrig/f)
     heightTh = Int(heightOrig/f)
     widthTh = Width
     heightTh = Height
  End If
  Call objCxImage.Resample(widthTh,heightTh,2)
  BinData = objCxImage.ImageForASP(2,Quality)
End If
' output in Response '
Response.BinaryWrite BinData
Function GetFileType(sFile)
  dot = InStrRev(sFile, ".")
  If dot > 0 Then sExt = LCase(Mid(sFile, dot + 1, 3))
  If sExt = "bmp" Then filetype = 0
  If sExt = "gif" Then filetype = 1
  If sExt = "jpg" Then filetype = 2
  If sExt = "png" Then filetype = 3
  If sExt = "ico" Then filetype = 4
  If sExt = "tif" Then filetype = 5
  If sExt = "tga" Then filetype = 6
  If sExt = "pcx" Then filetype = 7
End Function


The source code of the file is listed next. It is essentially a translation from C# to VBScript from my previously presented ASP.NET thumbnail user control. The output that the ASP.NET DataList control produces can be easily implemented by constructing an HTML table with code. The Columns parameter determines where the row (<tr>) tags are placed. However, the editing capabilities of the DataList control are much more difficult to be implemented with ASP code. For this reason, I did not implement thumbnail comments editing and presentation in this version.

Function ListThumbs(VPath,Columns,Filter,AllowPaging,PageSize,Width,Height,Quality,
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set ScriptName = Request.ServerVariables("SCRIPT_NAME")
  ScriptPath = Left(ScriptName,InstrRev(ScriptName,"/"))
  If Right(VPath,1)<>"/" Then VPath=VPath & "/"
  path = Server.MapPath(VPath):  If Right(path,1)<>"\" Then path=path & "\"
  ' analyze filter string
  If Len(Filter)=0 Then Filter = "*.gif;*.jpg;*.bmp"
  arSuffixes = Split(Filter,";")
  For i=0 To UBound(arSuffixes)
   If pos>0 Then arSuffixes(i) = Mid(arSuffixes(i),pos) ' e.g. ".gif"
  Dim CurPage,TotalPages
  ' -- write client script and hidden field for current page and path
  If AllowPaging Then Call WriteGotoPageScript
  If Len(Request.Form("hdnCurPage"))>0 Then
    CurPage = CInt(Request.Form("hdnCurPage"))
  Else CurPage=1
  End If
  Response.Write "<input type='hidden' name='hdnCurPage' value='" &_
                 CurPage & "' />" & vbCrLf
                ' restart page numbering if different page
  If Request.Form("hdnPrevPath")<> VPath Then CurPage = 1
  Response.Write "<input type='hidden' name='hdnPrevPath' value='" &_
                  VPath & "' />" & vbCrLf
  Dim arrFilesAll(),ubn
  If fso.FolderExists(path) Then
    Set oFolder = fso.GetFolder(path)
    For Each oFile in oFolder.Files
      For i=0 To UBound(arSuffixes)
        If IsFileOfType(oFile.Name,arSuffixes(i)) Then bFileInFilter=True
      If bFileInFilter Then
        ReDim Preserve arrFilesAll(ubn)
        arrFilesAll(ubn) = oFile.Name
      End If ' if in filter
   End If
  If AllowPaging Then
   TotalPages = CInt(ubn/PageSize)
   If ubn Mod PageSize>0 Then TotalPages = TotalPages + 1
   if TotalPages=0 Then TotalPages=1
    ' make sure current page is in the [1,totalPages] range
   if CurPage>TotalPages  Then
     if CurPage<1 Then CurPage=1
   end if
   ' range of files to read
   StartIndex = (CurPage-1)*PageSize
   EndIndex = CurPage*PageSize
   If ubn < EndIndex Then EndIndex = ubn
   StartIndex = 0: EndIndex = ubn
  End If
  ' write thumbnail hrefs
  If fso.FolderExists(path) Then
    Response.Write "<TABLE ALIGN='CENTER'>" & vbCrLf
    For index=StartIndex To EndIndex-1
      FileName = arrFilesAll(index)
      ' Response.write path & FileName
      Set oFile=fso.GetFile(path & FileName) : FileSize=oFile.Size
      nCol = nCol+1
      VFilePath = VPath & FileName
      sHREF = "<a href='" & VFilePath & "'>" & _
        "<img src='" & ScriptPath & "ThumbGenerate.asp?VFilePath=" & _
      Server.URLEncode(VFilePath) & "&Width=" &_
                  Width & "&Height=" & Height &_
                  "&Quality=" & Quality & _
                  "&ShellThumbnails=" & ShellThumbnails &_
                  "&AllowStretch=" & AllowStretch & _
                  "' border=0 alt='Name: " & FileName &_
                  "&#10;&#13;Size: " & CInt(FileSize/1024) &_
                  " Kb'/> </a>"
      If nCol = 1 Then Response.Write "<TR>"
       Response.Write "<TD align='Center' " & _
         "style='background-color:White;border-width:1px;" &_
         "border-style:Solid;font-size:8pt;'>" & vbCrLf
       Response.Write sHREF & vbCrLf
       If ShowFilenames Then Response.Write "<br/>" & FileName
       Response.Write "</TD>" & vbCrLf
       If nCol = Columns Then
         Response.Write "</TR>" & vbCrLf
           nCol = 0
       End If
     Response.Write NumImagesDisplayed(EndIndex-StartIndex,AllowPaging,ubn,_
     If AllowPaging Then
       Response.Write CreateNavBar(VPath,CurPage,TotalPages,Columns)
     End If
     Response.Write "</TABLE>" & vbCrLf
    Response.Write "Directory Does Not Exist<P>" & vbCrLf
  End If
End Function
Function NumImagesDisplayed(NumDisplayed,AllowPaging,TotalFiles,CurPage,TotalPages)
 s = "<tr><td align='Center' colspan='" & Columns &_
     "' style='background-color:#FFFFC0;font-size:8pt;'>"
 s = s & NumDisplayed
 if AllowPaging  Then s = s & " of " & TotalFiles
 ' s = s & " images"
 if AllowPaging Then
   s = s & "  (Page " & CurPage & " of " & TotalPages & ")"
 end if
 s =  s & "</td></tr>"
 NumImagesDisplayed = s
End Function
Function CreateNavBar(VPath,CurPage,TotalPages,Columns)
if TotalPages = 1 Then Exit Function
s = "<tr><td align='Center' colspan='" & Columns &_
    "' style='font-size:8pt;'>"
if CurPage>1 Then
  'GoToPage client script updates hidden field with target page
  'and then causes a postback
  s =  s & "<A href=""javascript:GoToPage('" & VPath &_
         "'," & CurPage-1 & _
   ");"">&lt;&lt; Prev</A>  "
End If
for i=1 To TotalPages
  if i=CurPage Then
    s =  s & "<b>" & i & "</b>"
    s =  s & "<A href=""javascript:GoToPage('" & VPath &_
                  "'," & i & ");"">" & _
     i & "</A>"
  end if
  s =  s & " "
  If i Mod 10=0 Then s = s & "<br/>"
If CurPage<TotalPages Then
  s =  s & " <A href=""javascript:GoToPage('" &_
          VPath & "'," & CurPage+1 & _
   ");"">Next &gt;&gt;</A>  "
End If
s =  s & "</td></tr>"
CreateNavBar = s
End Function
Function IsFileOfType(afile,infile_type)
  IsFileOfType = (UCase(Right(afile,Len(infile_type)))=UCase(infile_type))
End Function
Sub WriteGotoPageScript
 Response.Write "<script language=""javascript"">" & vbCrLf & _
  "//<!--" & vbCrLf & "function GoToPage(vpath,n) { " & vbCrLf & _
  "document.forms(0).hdnCurPage.value=n;" & vbCrLf & _
  "document.forms(0).submit(); " & vbCrLf & _
  "}" & vbCrLf & "//-->" & vbCrLf & "</" & "script>" & vbCrLf
End Sub

The GoToPage client JavaScript function that is "injected" in the HTML form implements page navigation. This function is executed at the client side when the user presses any of the page links. It sets the hdnCurPage hidden field's value equal to the argument n (that is the page number) and then submits the form. After postback, the page number is retrieved from the hidden field at page load.


I have not yet found on the Web any totally free ASP thumbnail solution, so I hope you'll find this article and the code useful. I recommend you to use the ASP.NET solution if you can, because it is faster and has more options. It seems that the imaging classes of .NET have optimized code. May be if you have a fast PC, you'll not notice the difference, but in my Pentium II, it is obvious.


  • 8th December, 2002: Initial post.
  • 31st October, 2009: Article updated.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Philipos Sakellaropoulos
Web Developer
Greece Greece
Software developer and Microsoft Trainer, Athens, Greece (MCT,, MCSE 2003, MCDBA 2000,MCTS, MCITP, MCIPD).

You may also be interested in...

Comments and Discussions

Questionregistering the dll file ? Pin
miller.j14-Nov-09 3:21
membermiller.j14-Nov-09 3:21 
AnswerRe: registering the dll file ? Pin
Philipos Sakellaropoulos18-Nov-09 10:41
memberPhilipos Sakellaropoulos18-Nov-09 10:41 
GeneralUpdated version using CxImage 6.00 - jpg,tif,png,gif support Pin
Philipos Sakellaropoulos31-Oct-09 15:17
memberPhilipos Sakellaropoulos31-Oct-09 15:17 
GeneralRe: Updated version using CxImage 6.00 - jpg,tif,png,gif support Pin
HeyCoda7-Nov-09 7:56
memberHeyCoda7-Nov-09 7:56 
AnswerRe: Updated version using CxImage 6.00 - jpg,tif,png,gif support Pin
Philipos Sakellaropoulos8-Nov-09 21:16
memberPhilipos Sakellaropoulos8-Nov-09 21:16 
GeneralRe: Updated version using CxImage 6.00 - jpg,tif,png,gif support Pin
Philipos Sakellaropoulos18-Nov-09 10:40
memberPhilipos Sakellaropoulos18-Nov-09 10:40 
GeneralRe: Updated version using CxImage 6.00 - jpg,tif,png,gif support Pin
HeyCoda18-Nov-09 13:36
memberHeyCoda18-Nov-09 13:36 
QuestionDoes this script generate true thumbnails? Pin
michaelmcguk21-Oct-09 1:19
membermichaelmcguk21-Oct-09 1:19 
AnswerRe: Does this script generate true thumbnails? Pin
HeyCoda23-Oct-09 6:14
memberHeyCoda23-Oct-09 6:14 
GeneralRe: Does this script generate true thumbnails? Pin
michaelmcguk23-Oct-09 6:53
membermichaelmcguk23-Oct-09 6:53 
GeneralRe: Does this script generate true thumbnails? Pin
HeyCoda23-Oct-09 7:24
memberHeyCoda23-Oct-09 7:24 
GeneralRe: Does this script generate true thumbnails? Pin
michaelmcguk24-Oct-09 6:51
membermichaelmcguk24-Oct-09 6:51 
GeneralRe: Does this script generate true thumbnails? Pin
HeyCoda25-Oct-09 14:01
memberHeyCoda25-Oct-09 14:01 
GeneralRe: Does this script generate true thumbnails? Pin
michaelmcguk25-Oct-09 23:51
membermichaelmcguk25-Oct-09 23:51 
GeneralRe: Does this script generate true thumbnails? Pin
HeyCoda28-Oct-09 18:48
memberHeyCoda28-Oct-09 18:48 
GeneralRe: Does this script generate true thumbnails? Pin
Philipos Sakellaropoulos31-Oct-09 16:25
memberPhilipos Sakellaropoulos31-Oct-09 16:25 
GeneralRe: Does this script generate true thumbnails? Pin
Philipos Sakellaropoulos31-Oct-09 16:23
memberPhilipos Sakellaropoulos31-Oct-09 16:23 
AnswerRe: Does this script generate true thumbnails? Pin
Philipos Sakellaropoulos31-Oct-09 16:11
memberPhilipos Sakellaropoulos31-Oct-09 16:11 
GeneralRe: Does this script generate true thumbnails? Pin
HeyCoda31-Oct-09 19:17
memberHeyCoda31-Oct-09 19:17 
General0x800A000D error Pin
HeyCoda20-Oct-09 9:38
memberHeyCoda20-Oct-09 9:38 
QuestionFirefox Issue Pin
dleonardmvs8-Sep-08 6:01
memberdleonardmvs8-Sep-08 6:01 
AnswerRe: Firefox Issue Pin
urobe28-Nov-08 3:00
memberurobe28-Nov-08 3:00 
GeneralError on Windows Server 2003 64x Pin
Deb Kozlovac3-Sep-08 6:20
memberDeb Kozlovac3-Sep-08 6:20 
QuestionError Type: Server object, ASP 0177 (0x800401F3) Pin
Member 289496930-Jul-08 6:02
memberMember 289496930-Jul-08 6:02 
AnswerRe: Error Type: Server object, ASP 0177 (0x800401F3) Pin
Member 289496930-Jul-08 8:57
memberMember 289496930-Jul-08 8:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web01 | 2.8.160208.1 | Last Updated 19 Nov 2009
Article Copyright 2002 by Philipos Sakellaropoulos
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid