Click here to Skip to main content
14,574,641 members

Thumbnail Image Viewer Control for ASP.NET 2.0

Rate this:
4.81 (50 votes)
Please Sign up or sign in to vote.
4.81 (50 votes)
24 May 2007
A thumbnail image viewer control which downloads and displays the full size image in modal form using a floating div and javascript.

Screenshot - ThumbViewerControl1.jpg


Generating thumbnail images and building a user interface for viewing the full size images can be time consuming. Using an HttpHandler to generate thumbnail images at runtime is a well-documented topic in numerous articles. The ThumbViewer control uses this technique, but its main feature is that it has a UI for downloading and viewing fullsize images built into the control.


I originally developed the UI functionality of the ThumbViewer control for viewing photos on my personnal website using JavaScript and HTML. By using embedded resources and adding an HttpHandler, it was quite easy to wrap this up as an ASP.NET control.

Design Objectives

The design objectives of the control:

  • Display a thumbnail image on a web page with a means of requesting the full image.
  • When the user requests the full size image, stream it to the browser and display it.
  • While viewing the full image, the original web page is not closed or does not need to be reloaded when the full image is closed.
  • Display the full image without using a separate browser window.
  • Allow the user to choose which full size images to view without having to view all images in a slideshow type UI.
  • Maximize the size of the full image for the browser window size.

Using the ThumbViewer Control

In the Toolbox in Visual Studio 2005 right-click and select "Choose Items...". On the Choose Toolbox Items dialog click on Browse and select the Bright.Controls.dll assembly. This will add the ThumbViewer control to the toolbox.

Screenshot - ThumbViewerControl2.jpg

To register the ThumbViewer HttpHandler with your application, add the following to the <system.web> section of your web.config. (This step is not necessary if you plan to use your own thumbnail images)

<!-- Register the ThumbViewer HttpHandler -->
      <add verb="GET" path="ThumbHandler.ashx" 

Drag the ThumbViewer control onto a page like any other web control and set its properties.

Screenshot - ThumbViewerControl3.jpg


  • ImageUrl - The URL of the full size image
  • ThumbUrl - The URL of the thumbnail image
  • Title - The Title of the image, this is displayed when viewing the fullsize image
  • ModalDisplayMode - The mode of the modal display, Stretch, Fixed or Disabled
  • ModalImagePadding - The padding of the modal display if ModalDisplayMode is set to Stretch
  • ModalFixedWidth - The width of the modal display if ModalDisplayMode is set to Fixed
  • ModalFixedHeight - The height of the modal display if ModalDisplayMode is set to Fixed

There are several ways to use the ThumbViewer Control to display images.

Here are the options depending on the types of images you want to display

You already have thumbnail images:

  • Set the ImageUrl to the full size image location and set the ThumbUrl to the thumbnail image location.

You don't have thumbnail images:

  • Set the ImageUrl to the full size image location and leave the ThumbUrl property empty, the thumbnail image will be generated at runtime.

You have small images which and you want to display a larger version of the same image:

  • Set the ThumbUrl to the full size image location and leave the ImageUrl property empty.

Here are the ModalDisplayModes for the full size image

Stretch is the default display mode and will utilise the full size of the browser window while keeping the same aspect ratio as the thumbnail image.

The Fixed display mode will display the image to the size specified in the ModalFixedWidth and ModalFixedHeight properties.

The Disabled display mode will not allow the full size image to be viewed.

How it Works

C# Code

The Bright.WebControls assembly contains two classes, ThumbViewer.cs and ThumbHandler.cs. The ThumbViewer.cs class is derived from the System.Web.UI.WebControls.Image control.

Some of the Image properties are overidden:

public override string ImageUrl {}
public override Unit Width {}
public override Unit Height {}

and some custom properties and an enum are added:

public virtual string ThumbUrl {}
public virtual string Title {}
public virtual Unit ModalImagePadding {}
public virtual Unit ModalFixedWidth {}
public virtual Unit ModalFixedHeight {}
public ModalDisplayModeOptions ModalDisplayMode {}

// ModalDisplayModeOptions enum
public enum ModalDisplayModeOptions

The OnInit and some of the Render methods are overidden to setup various aspects of the contol.

protected override void OnInit(EventArgs e)
    // Register Javascript and CSS files as the control is initialised

protected override void Render(HtmlTextWriter writer)
    // Setup up the ImageUrl & ThumbUrl

public override void RenderBeginTag(HtmlTextWriter writer)
    if (!DesignMode)
        // Before the first control, write out the Modal Divs
protected override void AddAttributesToRender(HtmlTextWriter writer)
    if (!DesignMode)
        // Setup the image Attributes

  • OnInit calls SetupIncludes(); which registers the JavaScript and stylesheet resources with the page.
  • Render calls SetupUrls(); which modifies the src of the image as required.
  • RenderBeginTag calls SetupModal(writer); which adds the HTML for the modal. The modal is only added for the first control on the page and shared by all the others.
  • AddAttributesToRender calls SetupAttributes(writer); which adds the Onclick event handler to the images.

The private methods are well commented in the source code download so I won't go into detail here.

The ThumbHandler.cs class implements the IHttpHandler interface.

public class ThumbHandler : IHttpHandler {}

The thumbnail image creation is handled in the ProcessRequest method.

public void ProcessRequest(HttpContext context)
    // Get the QueryString parameters passed
    string _imagePath = context.Request.QueryString["i"] == null ?
        string.Empty : context.Request.QueryString["i"].ToString();
    int _thumbWidth = context.Request.QueryString["w"] == null ?
        100 : int.Parse(context.Request.QueryString["w"].ToString());
    int _thumbHeight = context.Request.QueryString["h"] == null ?
        100 : int.Parse(context.Request.QueryString["h"].ToString());

    // Create a thumb image from the source image
    System.Drawing.Image _sourceImage = System.Drawing.Image.FromFile(
    System.Drawing.Image _thumbImage = this.CreateThumbnail(
        _sourceImage, _thumbWidth, _thumbHeight);

    // Save the thumb image to the OutputStream
    _thumbImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
private System.Drawing.Image CreateThumbnail(System.Drawing.Image image, 
    int thumbWidth, int thumbHeight)
    return image.GetThumbnailImage(
        delegate() { return false; }, 

The querystring parameters are retrieved and then the image is passed to CreateThumbnail to generate the thumbnail with the dimensions passed. The thumbnail image is then saved to the OutputStream.

Embedded Resources

The embedded resources are contained in the Resources folder:

  • ThumbViewer.js - Handles the modal display of the full size image.
  • ThumbViewer.css - Contains the styles for the modal display.
  • Progress.gif - Animated image which is displayed while the full size image is loading.

These resources are registered in the AssemblyInfo.cs class.

[assembly: WebResource("Bright.WebControls.Resources.ThumbViewer.css",
[assembly: WebResource("Bright.WebControls.Resources.ThumbViewer.js",
    "text/javascript", PerformSubstitution = true)]
[assembly: WebResource("Bright.WebControls.Resources.Progress.gif",


The control has been tested on the following browsers: IE 7.0, Firefox 2.0, Opera 9.1 and with the following image formats: JPG, GIF, PNG, BMP.



By extending the System.Web.UI.WebControls.Image control, adding some JavaScript and an HttpHandler you can create a nice solution for viewing thumbnails and fullsize images in a web application.


  • v1.0 - 11th Feb 2007
  • v1.1 - 18th Feb 2007
    The progress bar image is displayed correctly. The fullsize image modal is repositioned if page is resized or scrolled.


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

Declan Bright
Ireland Ireland
I have been designing and developing business solutions for the aviation and telecommunications industries since 1999. My experience covers a wide range of technologies and I have delivered a variety of web and mobile based solutions.

Comments and Discussions

QuestionMy vote is 5 Pin
mohamad yousef2-May-12 21:29
Membermohamad yousef2-May-12 21:29 
GeneralMy vote of 5 Pin
Dean Oliver1-Feb-12 18:16
MemberDean Oliver1-Feb-12 18:16 
QuestionProblem in master page Pin
phuongmc31-Aug-11 21:05
Memberphuongmc31-Aug-11 21:05 
GeneralMy vote of 5 Pin
TweakBird15-Mar-11 9:10
MemberTweakBird15-Mar-11 9:10 
GeneralMy vote of 5 Pin
Sandeep Mewara6-Feb-11 6:37
mentorSandeep Mewara6-Feb-11 6:37 
GeneralDelay to load image Pin
Saeed.39427-Jun-10 2:17
MemberSaeed.39427-Jun-10 2:17 
GeneralDelay to load image Pin
Saeed.39424-Jun-10 1:33
MemberSaeed.39424-Jun-10 1:33 
GeneralSetting the ImageUrl Pin
Chamara Janaka19-Jun-10 1:15
MemberChamara Janaka19-Jun-10 1:15 
GeneralFramework 3.5 Pin
surgeon765-Mar-10 10:02
Membersurgeon765-Mar-10 10:02 
GeneralNot framework issue Pin
surgeon765-Mar-10 11:43
Membersurgeon765-Mar-10 11:43 
GeneralMy vote of 2 Pin
skippy20051-Mar-10 3:57
Memberskippy20051-Mar-10 3:57 
GeneralProblem with Server.MapPath Pin
Afshar Mohebbi16-Jan-10 22:34
MemberAfshar Mohebbi16-Jan-10 22:34 
GeneralRe: Problem with Server.MapPath Pin
Saeed.39416-Jul-10 17:54
MemberSaeed.39416-Jul-10 17:54 
GeneralRe: Problem with Server.MapPath Pin
Afshar Mohebbi17-Jul-10 3:46
MemberAfshar Mohebbi17-Jul-10 3:46 
QuestionHow to Display Big Image in Original Size???? Pin
crazyhemant12314-Nov-09 0:03
Membercrazyhemant12314-Nov-09 0:03 
AnswerRe: How to Display Big Image in Original Size???? Pin
Declan Bright14-Nov-09 7:22
MemberDeclan Bright14-Nov-09 7:22 
GeneralRe: How to Display Big Image in Original Size???? Pin
mecrazyhemant15-Nov-09 21:51
Membermecrazyhemant15-Nov-09 21:51 
GeneralMy vote of 1 Pin
jojotjo16-Aug-09 14:00
Memberjojotjo16-Aug-09 14:00 
Generalnot valid virtual path Pin
fansyari23-Jul-09 23:32
Memberfansyari23-Jul-09 23:32 
Questionuse thumbviewer inside another control Pin
Mohamad Nazari17-Jul-09 17:01
MemberMohamad Nazari17-Jul-09 17:01 
GeneralProblems in IE8 RC1 Pin
goldmd27-Feb-09 9:31
Membergoldmd27-Feb-09 9:31 
GeneralVirtual directory Pin
NewbieAE29-Jan-09 10:12
MemberNewbieAE29-Jan-09 10:12 
GeneralThe full sized image does nto exist Pin
Chronojesster23-Jan-09 2:32
MemberChronojesster23-Jan-09 2:32 
GeneralRe: The full sized image does nto exist Pin
Chronojesster23-Jan-09 2:56
MemberChronojesster23-Jan-09 2:56 
GeneralRe: The full sized image does nto exist Pin
coltex_marek9-Mar-09 7:52
Membercoltex_marek9-Mar-09 7:52 

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.

Posted 11 Feb 2007


202 bookmarked