To avoid this problem, you simply create a small image called a thumbnail on the fly using GDI+.
What you have to do is just create a handler in your ASP.NET project and put the following code inside the handler. This code uses Image and Bitmap classes defined in GDI+ to create a small image and sets the quality and encoders of the image.
<%@ WebHandler Language="C#" Class="ThumbnailHandler" %>
using System;
using System.IO;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web.Caching;
using System.Configuration;
public class ThumbnailHandler : IHttpHandler
{
// ----- Variable declaration -------
int thumbWidth,thumbHeight; String _path;
bool bRefresh=true;
public void ProcessRequest (HttpContext context)
{
String sCacheKey;
bool bFoundInCache = false;
// by default
if(context.Request["Width"]!=null)
thumbWidth = Int32.Parse(context.Request["Width"]);
else {
try { thumbWidth = int.Parse(ConfigurationSettings.AppSettings["DefaultWidth"]); }
catch (ArgumentNullException ex) { thumbWidth = 50; }
}
if(context.Request["Height"]!=null)
thumbHeight = Int32.Parse(context.Request["Height"]);
else {
try { thumbHeight = int.Parse(ConfigurationSettings.AppSettings["DefaultHeight"]); }
catch (ArgumentNullException ex) { thumbHeight = 40; }
}
// get path of 'no thumbnail' image
const String NoThumbFile = "Images/noImageAvailable.jpg";
String sNoThumbPath = context.Request.MapPath(
context.Request.ApplicationPath.TrimEnd('/') + "/" + NoThumbFile);
// map requested path
if(context.Request["VFilePath"]!=null)
if (context.Request["VFilePath"].IndexOf(".jpeg") != -1 || context.Request["VFilePath"].IndexOf(".jpg") != -1 || context.Request["VFilePath"].IndexOf(".gif") != -1 || context.Request["VFilePath"].IndexOf(".bmp") != -1 || context.Request["VFilePath"].IndexOf(".png") != -1 || context.Request["VFilePath"].IndexOf(".JPEG") != -1 || context.Request["VFilePath"].IndexOf(".JPG") != -1 || context.Request["VFilePath"].IndexOf(".GIF") != -1 || context.Request["VFilePath"].IndexOf(".BMP") != -1 || context.Request["VFilePath"].IndexOf(".PNG") != -1)
_path = context.Request.MapPath(context.Request["VFilePath"]);
else
_path = sNoThumbPath;
//======================================
System.Drawing.Image image;
try
{
image = System.Drawing.Image.FromFile(_path);
}
catch (FileNotFoundException e)
{
image = System.Drawing.Image.FromFile(sNoThumbPath);
}
int srcWidth = image.Width;
int srcHeight = image.Height;
// if(srcHeight>srcWidth)
// thumbHeight=(srcHeight/srcWidth)*thumbWidth;
// else
// thumbHeight=(srcWidth/srcHeight)*thumbWidth;
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(thumbWidth, (int)thumbHeight);
//- Create a System.Drawing.Graphics object from the Bitmap which we will use to draw the high quality scaled image
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bitmap);
//- Set the System.Drawing.Graphics object property SmoothingMode to HighQuality
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//- Set the System.Drawing.Graphics object property CompositingQuality to HighQuality
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//- Set the System.Drawing.Graphics object property InterpolationMode to High
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//- Draw the original image into the target Graphics object scaling to the desired width and height
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, (int)thumbHeight);
//Set Image codec of JPEG type, the index of JPEG codec is "1"
System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1];
//Set the parameters for defining the quality of the thumbnail... here it is set to 100%
System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1);
eParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, System.Drawing.GraphicsUnit.Pixel);
//======================================
}
Save the above ASP.NET file as ThumbnailHandler.ashx.
Now your webpage is ready to shine with great quality thumbnails.
This is how you an use the above ASHX file.
1. For HTML image tag:
<img src="ThumbnailHandler.ashx?VFilePath=../image/Myimage.jpg & Height=230 & Width=340" alt="Thumbnailimage" />
2. For ASP.NET Image tag:
<asp:Image id="Image1" runat="server" ImageUrl="ThumbnailHandler.ashx?VFilePath=~/Images/Myimage.jpg & Height=230 & Width=340" AlternateText="Thumbnailimage" />
I think situation is more clear to you now so start using it and enjoy.
Give feedback to keep my article live.

John HazinPosted Jul 23, 2010, 1:11 PM
Hi Amit here is the mark up call <asp:Image ID="imgThumb" runat="server" ImageUrl="ThumbnailHandler.ashx?VFilePath=~/documents/pF232167k8/test.jpg & Height=230 & Width=340" AlternateText="Thumbnailimage" /> here is the source for a test image: <img id="ctl00_CPHContent_rptUploadedDocuments_ctl01_gvDocs_ctl02_imgThumb" src="ThumbnailHandler.ashx?VFilePath=~/documents/pF232167k8/test.jpg & Height=230 & Width=340" alt="Thumbnailimage" style="border-width:0px;" /> I get the standard broken image. please help John
John HazinPosted Jul 19, 2010, 4:20 PM
Hi Amit. Thanks your for help. how did you register the handler? What do I add in the web.config file if anything? or Is it done in IIS 7? Thanks. John
Amit ChoudharyPosted Jul 16, 2010, 6:28 AM
hi john, IsReusable is an Menthod of IHttpHandler interface and we need to implement. ya i forget to mention it here. It will be like this: public bool IsReusable { get { return false; } } 2. User the handler with your Hyperlink like this: <asp:HyperLink ImageUrl='<%#"ThumbnailHandler.ashx?VFilePath=~/documents/"+Eval("folder")+"/"+Eval("filename")+"&Height=230&Width=340"%>' ................./> if it won't work then put an <asp:Image ImageUrl='<%.....%>' ../> between opening and closing tag of <asp:HyperLink....> // here you image goes</asp:HyperLink> Hope all your issues solved now.
John HazinPosted Jul 15, 2010, 6:41 PM
Hi Very Nice article. However I"m having to issues 1- when I compile I get: Error 50 'ThumbnailHandler' does not implement interface member 'System.Web.IHttpHandler.IsReusable' D:\webprojects\eDENT\www\ThumbnailHandler.ashx 2- Instead of an image I'm using an ASP.Net hyperlink. Will this still work ? I get a tag is not formated well error. here is the markup <asp:HyperLink ID="hyprGridDocLink" runat="server" Text='<%# Eval("filename") %>' ImageUrl="ThumbnailHandler.ashx?VFilePath=" + '<%# "~/documents/" + Eval("folder") +"/" + Eval("filename") %>' + " & Height=230 & Width=340" NavigateUrl='<%# "~/documents/" + Eval("folder") +"/" + Eval("filename") %>' Target="_blank" /> Thanks a ton for any help john