<%@ 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; using PhilipSoft.Thumbnails; public class ThumbnailHandler : IHttpHandler { // ----- Copyright Philipos Sakellaropoulos 2002 ------- int _width,_height; String _path; bool _bStretch,_bBevel,_bUseCOMobject; ThumbGenerator _oGenerator; public void ProcessRequest (HttpContext context) { Bitmap bitmap; String sCacheKey; bool bFoundInCache = true; // by default // create our COM thumbnail generator _oGenerator = new ThumbGenerator(); // get width and height if(context.Request["Width"]!=null) _width = Int32.Parse(context.Request["Width"]); else { try {_width = int.Parse(ConfigurationSettings.AppSettings["DefaultWidth"]); } catch(ArgumentNullException ex) { _width=150; } } if(context.Request["Height"]!=null) _height = Int32.Parse(context.Request["Height"]); else { try {_height = int.Parse(ConfigurationSettings.AppSettings["DefaultHeight"]); } catch(ArgumentNullException ex) { _height=150; } } // get path of 'no thumbnail' image const String NoThumbFile = "NoThumb.gif"; //"victory.jpg"; String sNoThumbPath = context.Request.MapPath( context.Request.ApplicationPath.TrimEnd('/') + "/" + NoThumbFile); // map requested path if(context.Request["VFilePath"]!=null) _path = context.Request.MapPath(context.Request["VFilePath"]); else _path = sNoThumbPath; //_path = context.Request.MapPath("/images/OLIMP011.jpg"); //-->for test // allow stretch of thumbnails _bStretch = (context.Request["AllowStretch"]=="true"); // bevel thumbnails _bBevel = (context.Request["Bevel"]=="true"); _bUseCOMobject = (context.Request["UseCOMobj"]=="true"); // put parameters for thumbnail requested _oGenerator.SetParams(_path,_width,_height,_bStretch,_bBevel,_bUseCOMobject); // get a reference to the cache object Cache MyCache = context.Cache; sCacheKey = _oGenerator.GetUniqueThumbName(); // --- remove from cache when we want to refresh bool bRefresh = (context.Request["Refresh"]=="true"); if(bRefresh) MyCache.Remove(sCacheKey); if(MyCache[sCacheKey] == null) { // the thumbnail does not exist in cache, create it... // Create a bitmap of the thumbnail and show it try { bitmap = _oGenerator.ExtractThumbnail(); bFoundInCache=false; } catch(Exception e) { // requested image cannot be loaded, try to load the 'NoThumb' image _path = sNoThumbPath; _oGenerator.SetParams(_path,_width,_height,_bStretch,false,_bUseCOMobject); sCacheKey = _oGenerator.GetUniqueThumbName(); if(MyCache[sCacheKey]==null) { try { bitmap = _oGenerator.ExtractThumbnail(); bFoundInCache=false; } catch(Exception e2) { return; } } else bitmap = (Bitmap)MyCache[sCacheKey]; } // end catch } else { // bitmap is in cache bitmap = (Bitmap)MyCache[sCacheKey]; } context.Response.ContentType = "image/Jpeg"; bitmap.Save (context.Response.OutputStream, ImageFormat.Jpeg); //if(bFoundInCache) LogMessage("Found in cache"); // else LogMessage("NOT Found in cache"); //cache thumbnail, make it dependent upon the file and thumbnail size bool bUseCache = !(ConfigurationSettings.AppSettings["UseCache"]=="false"); if(!bFoundInCache && bUseCache) { CacheDependency dependency = new CacheDependency(_path); int mins; try { mins = int.Parse(ConfigurationSettings.AppSettings["SlidingExpireMinutes"]); } catch(ArgumentNullException ex) { mins=20; } MyCache.Insert(sCacheKey, bitmap ,dependency, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(mins), CacheItemPriority.Default, new CacheItemRemovedCallback(RemovedCallback)); dependency.Dispose(); } // bitmap in cache, dont dispose yet //bitmap.Dispose (); } static public void RemovedCallback(String k, Object item, CacheItemRemovedReason r){ ((Bitmap)item).Dispose(); //LogMessage("Callback"); } // for custom tracing, normal tracing does not work with WebHandlers static void LogMessage(String mess) { StreamWriter sw = new StreamWriter("c:\\ASP.NET_log.txt",true); sw.WriteLine(mess); sw.Close(); } public bool IsReusable { get { return true; } } }