Click here to Skip to main content
15,896,557 members
Articles / Web Development / ASP.NET

An ASP.NET thumbnail solution

Rate me:
Please Sign up or sign in to vote.
4.61/5 (52 votes)
10 Dec 2002Ms-RL12 min read 599.8K   8.5K   266  
An article describing a set of ASP.NET tools for generation of thumbnails and thumbnail views.
// Copyright Philipos Sakellaropoulos 2002

namespace PhilipSoft.Thumbnails {
    using System;
    using System.Text;
    using System.IO;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Drawing.Drawing2D;
    using THUMBEXTRACTLib; // our COM object wrapper

    /// <summary>
    /// Uses the shell interface IImageExtract2
    /// </summary>
public class ThumbGenerator {
   private String _path;
   private int _width,_height;
   private bool _bStretch,_bBevel,_bUseCOMobject;
   private IFileThumbExtract _oCOMThumb;  
        /// <summary>
        /// Creates a new instance of ThumbGenerator<br/>
        /// bUseCOMobject: If use COM object to create thumbnails
        /// </summary>

   public void SetParams(String path,int width,int height,
      bool bStretch,bool bBevel,bool bUseCOMobject) {
     // cache parameters
      _path = path; _width= width; _height=height;
      _bStretch=bStretch; _bBevel=bBevel; _bUseCOMobject=bUseCOMobject;
   }   
   
        /// <summary>
        /// Extracts thumbnail based on physical image path, width, height
        /// </summary>
        /// <param>path</param>
        /// <param>width</param>
        /// <param>height</param>
        /// <param>bStretch</param>
        /// <param>bBevel</param>
    public Bitmap ExtractThumbnail()
    {
     Bitmap bitmapNew; float fx,fy,f; 
     int widthTh,heightTh; float widthOrig,heightOrig;
     if(_bUseCOMobject) {
       if(_oCOMThumb==null) _oCOMThumb = new FileThumbExtract();
       _oCOMThumb.SetPath(_path);
       _oCOMThumb.SetThumbnailSize(_width,_height);
       IntPtr hBitmap = (IntPtr)_oCOMThumb.ExtractThumbnail();
       bitmapNew = Bitmap.FromHbitmap(hBitmap);
       _oCOMThumb.FreeThumbnail();
      }
      else {
      // create thumbnail using .net function GetThumbnailImage
       bitmapNew = new Bitmap(_path); // load original image
       if(!_bStretch) { // retain aspect ratio
         widthOrig = bitmapNew.Width;
         heightOrig = bitmapNew.Height;
         fx = widthOrig/_width;
         fy = heightOrig/_height; // subsample factors
         // must fit in thumbnail size
         f=Math.Max(fx,fy); if(f<1) f=1;
         widthTh = (int)(widthOrig/f); heightTh = (int)(heightOrig/f);
       }
       else {
        widthTh = _width; heightTh = _height;
       }
       bitmapNew = (Bitmap)bitmapNew.GetThumbnailImage(widthTh, heightTh,
         new Image.GetThumbnailImageAbort(ThumbnailCallback),IntPtr.Zero);
      }
      if(!_bBevel) return bitmapNew;
      // ---- apply bevel
      int widTh,heTh;
      widTh = bitmapNew.Width; heTh = bitmapNew.Height;
      int BevW = 10, LowA=0, HighA=180, Dark=80, Light=255;
      // hilight color, low and high
      Color clrHi1 = Color.FromArgb(LowA,Light,Light,Light);
      Color clrHi2 = Color.FromArgb(HighA,Light,Light,Light);
      Color clrDark1 = Color.FromArgb(LowA,Dark,Dark,Dark);
      Color clrDark2 = Color.FromArgb(HighA,Dark,Dark,Dark);
      LinearGradientBrush br; Rectangle rectSide;
      Graphics newG = Graphics.FromImage(bitmapNew);   
      Size szHorz = new Size(widTh,BevW);
      Size szVert = new Size(BevW,heTh);
      // ---- draw dark (shadow) sides first
      // draw bottom-side of bevel
      szHorz+=new Size(0,2); szVert+=new Size(2,0);
      rectSide = new Rectangle(new Point(0,heTh-BevW),szHorz);
      br = new LinearGradientBrush(
        rectSide,clrDark1,clrDark2,LinearGradientMode.Vertical);
      rectSide.Inflate(0,-1);
      newG.FillRectangle(br,rectSide);  
      // draw right-side of bevel
      rectSide = new Rectangle(new Point(widTh-BevW,0),szVert);
      br = new LinearGradientBrush(
        rectSide,clrDark1,clrDark2,LinearGradientMode.Horizontal);
      rectSide.Inflate(-1,0);
      newG.FillRectangle(br,rectSide);  
      // ---- draw bright (hilight) sides next
      szHorz-=new Size(0,2); szVert-=new Size(2,0);
      // draw top-side of bevel
      rectSide = new Rectangle(new Point(0,0),szHorz);
      br = new LinearGradientBrush(
        rectSide,clrHi2,clrHi1,LinearGradientMode.Vertical);
      newG.FillRectangle(br,rectSide);  
      // draw left-side of bevel
      rectSide = new Rectangle(new Point(0,0),szVert);
      br = new LinearGradientBrush(
        rectSide,clrHi2,clrHi1,LinearGradientMode.Horizontal);
      newG.FillRectangle(br,rectSide);  
      // dispose graphics objects and return bitmap
      br.Dispose(); newG.Dispose();
      return bitmapNew;
    }
    
    public String GetUniqueThumbName() {
      StringBuilder sUN = new StringBuilder();
      sUN.AppendFormat("Thumb_{0}_{1}x{2}",
        Path.GetFileName(_path),_width,_height);
      if(_bStretch) sUN.Append("_S");
      if(_bBevel) sUN.Append("_B");
      if(_bUseCOMobject) sUN.Append("_COM");
      sUN.Append(".jpg");
      return sUN.ToString();
     }
  
        /// <summary>
        /// Saves a thumbnail in JPEG format
        /// </summary>
    public void SaveThumbnail(Bitmap bmThumb,String path)
    { 
       bmThumb.Save(path,ImageFormat.Jpeg);
    }

    public bool ThumbnailCallback() { return false; }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under Microsoft Reciprocal License


Written By
Web Developer Forthnet
Greece Greece
Software developer and Microsoft Trainer, Athens, Greece (MCT, MCSD.net, MCSE 2003, MCDBA 2000,MCTS, MCITP, MCIPD).

Comments and Discussions