Click here to Skip to main content
15,113,225 members
Articles / Web Development / ASP.NET
Article
Posted 11 Feb 2007

Stats

565K views
12.9K downloads
202 bookmarked

Thumbnail Image Viewer Control for ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
4.81/5 (50 votes)
24 May 20075 min read
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

Introduction

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.

Background

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)

C#
<!-- Register the ThumbViewer HttpHandler -->
    <httpHandlers>
      <add verb="GET" path="ThumbHandler.ashx" 
          type="Bright.WebControls.ThumbHandler"/>
    </httpHandlers>

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

Screenshot - ThumbViewerControl3.jpg

Properties

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

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

Disabled
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:

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

and some custom properties and an enum are added:

C#
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
{
    Stretch,
    Fixed,
    Disabled
}

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

C#
protected override void OnInit(EventArgs e)
{
    // Register Javascript and CSS files as the control is initialised
    SetupIncludes();

    base.OnInit(e);
}
protected override void Render(HtmlTextWriter writer)
{
    // Setup up the ImageUrl & ThumbUrl
    SetupUrls();

    base.Render(writer);
}
public override void RenderBeginTag(HtmlTextWriter writer)
{
    if (!DesignMode)
    {
        // Before the first control, write out the Modal Divs
        SetupModal(writer);
    }
    base.RenderBeginTag(writer);
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
    if (!DesignMode)
    {
        // Setup the image Attributes
        SetupAttributes(writer);
    }

    base.AddAttributesToRender(writer);
}
  • 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.

C#
public class ThumbHandler : IHttpHandler {}

The thumbnail image creation is handled in the ProcessRequest method.

C#
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(
        context.Server.MapPath(_imagePath));
    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(
        thumbWidth,
        thumbHeight, 
        delegate() { return false; }, 
        IntPtr.Zero
        );
}

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.

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

Testing

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.

References

Conclusion

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.

History

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

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

Share

About the Author

Declan Bright
Architect
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

 
GeneralRe: Modal window always showing in Landscape - won't show in portrait Pin
jimibt3-May-07 9:50
Memberjimibt3-May-07 9:50 
GeneralRe: Modal window always showing in Landscape - won't show in portrait Pin
Declan Bright9-May-07 9:50
MemberDeclan Bright9-May-07 9:50 
GeneralRe: Modal window always showing in Landscape - won't show in portrait Pin
jimibt11-May-07 7:48
Memberjimibt11-May-07 7:48 
GeneralRe: Modal window always showing in Landscape - won't show in portrait Pin
jimibt12-May-07 3:08
Memberjimibt12-May-07 3:08 
GeneralRe: Modal window always showing in Landscape - won't show in portrait Pin
yannlh24-Dec-07 10:39
Memberyannlh24-Dec-07 10:39 
GeneralRe: Modal window always showing in Landscape - won't show in portrait Pin
yannlh24-Dec-07 11:25
Memberyannlh24-Dec-07 11:25 
GeneralRe: Modal window always showing in Landscape - won't show in portrait Pin
yannlh2-Jan-08 9:39
Memberyannlh2-Jan-08 9:39 
GeneralModified the demo to run with paging on repeater Pin
jimibt1-May-07 7:06
Memberjimibt1-May-07 7:06 
Really great and useful control (and nice simple demo too!!).

I modified the demo to include a PagedDataSource which can point to the existing files object as well as any xml or sql datasource that would be used.

i've put a link to it below for the timebeing, and perhaps if it's deemed useful and doesn't detract from the simplicity of the demo, could be amalgamated into the demo. the code was modified from a version found at: http://aspnet.4guysfromrolla.com/articles/081804-1.aspx[^]

anyway, thanks again

jimi

my sourcecode demo extension is here:

http://www.worldindependentnews.com/files/ThumbViewerDemo.zip[^]

to show the demo in action, i've deliberately set the PageSize to a small number (3 in this case). In reality, the average page might be set to a PageSize of 20 (ish).
GeneralRe: Modified the demo to run with paging on repeater Pin
jimibt1-May-07 7:27
Memberjimibt1-May-07 7:27 
GeneralRe: Modified the demo to run with paging on repeater Pin
Declan Bright9-May-07 9:40
MemberDeclan Bright9-May-07 9:40 
GeneralFull Image does not appear when clicked Pin
sezhian_vertical30-Apr-07 1:23
Membersezhian_vertical30-Apr-07 1:23 
GeneralRe: Full Image does not appear when clicked Pin
Declan Bright30-Apr-07 3:51
MemberDeclan Bright30-Apr-07 3:51 
GeneralThanks Declan Pin
Elvisvr27-Apr-07 23:06
MemberElvisvr27-Apr-07 23:06 
GeneralRe: Thanks Declan Pin
Declan Bright29-Apr-07 7:24
MemberDeclan Bright29-Apr-07 7:24 
QuestionWhat about ModalDisplayMode to take the original Image size? Pin
Hasan Bazerbashi19-Mar-07 3:11
MemberHasan Bazerbashi19-Mar-07 3:11 
AnswerRe: What about ModalDisplayMode to take the original Image size? Pin
Declan Bright20-Mar-07 10:30
MemberDeclan Bright20-Mar-07 10:30 
GeneralRe: What about ModalDisplayMode to take the original Image size? Pin
yannlh19-Dec-07 16:36
Memberyannlh19-Dec-07 16:36 
GeneralRe: What about ModalDisplayMode to take the original Image size? Pin
yannlh24-Dec-07 11:22
Memberyannlh24-Dec-07 11:22 
Generalthanks Pin
dpobla4-Mar-07 5:34
Memberdpobla4-Mar-07 5:34 
GeneralRe: thanks Pin
dpobla4-Mar-07 5:54
Memberdpobla4-Mar-07 5:54 
GeneralRe: thanks Pin
dpobla4-Mar-07 8:33
Memberdpobla4-Mar-07 8:33 
GeneralRe: thanks [modified] Pin
Declan Bright5-Mar-07 22:23
MemberDeclan Bright5-Mar-07 22:23 
Generalmodifying path at runtime Pin
yannlh24-Feb-07 17:40
Memberyannlh24-Feb-07 17:40 
GeneralRe: modifying path at runtime Pin
yannlh25-Feb-07 8:27
Memberyannlh25-Feb-07 8:27 
GeneralRe: modifying path at runtime Pin
Declan Bright25-Feb-07 9:48
MemberDeclan Bright25-Feb-07 9:48 

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.