|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionGenerating 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. BackgroundI 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 ObjectivesThe design objectives of the control:
Using the ThumbViewer ControlIn 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.
To register the ThumbViewer HttpHandler with your application, add the following to the <!-- 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.
Properties
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 displayYou already have thumbnail images:
You don't have thumbnail images:
You have small images which and you want to display a larger version of the same image:
Here are the ModalDisplayModes for the full size imageStretch Fixed Disabled How it WorksC# CodeThe 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
{
Stretch,
Fixed,
Disabled
}
The 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);
}
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 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 Embedded ResourcesThe embedded resources are contained in the Resources folder:
These resources are registered in the AssemblyInfo.cs class. [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")]
TestingThe 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
ConclusionBy extending the History
| ||||||||||||||||||||