你的位置:首页 > ASP.net教程

[ASP.net教程]C#生成图片缩略图(2种思路)


 前言:在日常图片浏览中,如果图片过多,只有一张张的打开图片才能知道图片的内容,显然这样浏览起来非常不便。Windows系统在浏览图片时提供了缩略图的功能,这样大大的方便了浏览者了解每张图片的内容,本实例采用两种方式实现了与Windows系统缩略图相同的功能。

第一种:生成MakeThumbnail方法

 1 //// <summary>  2 /// 生成缩略图  3 /// </summary>  4 /// <param name="originalImagePath">源图路径(物理路径)</param>  5 /// <param name="thumbnailPath">缩略图路径(物理路径)</param>  6 /// <param name="width">缩略图宽度</param>  7 /// <param name="height">缩略图高度</param>  8 /// <param name="mode">生成缩略图的方式</param>    9 public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode) 10 { 11   Image originalImage = Image.FromFile(originalImagePath); 12 13   int towidth = width; 14   int toheight = height; 15     16   int x = 0; 17   int y = 0; 18   int ow = originalImage.Width; 19   int oh = originalImage.Height;     20 21   switch (mode) 22   {     23     case "HW"://指定高宽缩放(可能变形)         24     break; 25     case "W"://指定宽,高按比例           26     toheight = originalImage.Height * width/originalImage.Width; 27     break; 28     case "H"://指定高,宽按比例 29     towidth = originalImage.Width * height/originalImage.Height;           30     break; 31     case "Cut"://指定高宽裁减(不变形)    32      if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight) 33       { 34         oh = originalImage.Height; 35         ow = originalImage.Height*towidth/toheight; 36         y = 0; 37       } 38       else 39       { 40         ow = originalImage.Width; 41         oh = originalImage.Width*height/towidth; 42         x = 0; 43         y = (originalImage.Height - oh)/2; 44       } 45     break;           46     default : 47     break; 48   }   49       50   //新建一个bmp图片 51   Image bitmap = new System.Drawing.Bitmap(towidth,toheight); 52 53   //新建一个画板 54   Graphics g = System.Drawing.Graphics.FromImage(bitmap); 55 56   //设置高质量插值法 57   g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 58 59   //设置高质量,低速度呈现平滑程度 60   g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 61 62   //清空画布并以透明背景色填充 63   g.Clear(Color.Transparent);     64 65   //在指定位置并且按指定大小绘制原图片的指定部分 66   g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow,oh),GraphicsUnit.Pixel);67   try 68   {       69     //以jpg格式保存缩略图 70     bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); 71   } 72   catch(System.Exception e) 73   { 74    throw e; 75   } 76   finally 77   { 78     originalImage.Dispose(); 79     bitmap.Dispose();             80     g.Dispose(); 81   } 82 }

ThumbnailClass

第二种:写成ThumbnailClass类,书写4个重载方法,有直接返回Image对象的,有生成缩略图,并且保存到指定目录等等

 1 using System.IO; 2 using System.Drawing; 3 using System.Drawing.Imaging; 4  5 /// <summary> 6 /// 图片处理类 7 /// 1、生成缩略图片或按照比例改变图片的大小和画质 8 /// 2、将生成的缩略图放到指定的目录下 9 /// </summary> 10 public class ImageClass 11 { 12   public Image ResourceImage; 13   private int ImageWidth; 14   private int ImageHeight; 15  16   public string ErrMessage; 17  18   /// <summary> 19   /// 类的构造函数 20   /// </summary> 21   /// <param name="ImageFileName">图片文件的全路径名称</param> 22   public ImageClass(string ImageFileName) 23   { 24     ResourceImage=Image.FromFile(ImageFileName); 25   ErrMessage=""; 26   } 27  28   public bool ThumbnailCallback() 29   { 30   return false; 31   } 32  33   /// <summary> 34   /// 生成缩略图重载方法1,返回缩略图的Image对象 35   /// </summary> 36   /// <param name="Width">缩略图的宽度</param> 37   /// <param name="Height">缩略图的高度</param> 38   /// <returns>缩略图的Image对象</returns> 39   public Image GetReducedImage(int Width,int Height) 40   { 41   try 42    { 43    Image ReducedImage; 44  45    Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);  46    47    ReducedImage=ResourceImage.GetThumbnailImage(Width,Height,callb,IntPtr.Zero); 48   49    return ReducedImage; 50    } 51   catch(Exception e) 52    { 53    ErrMessage=e.Message;  54     return null; 55    } 56   } 57  58   /// <summary> 59   /// 生成缩略图重载方法2,将缩略图文件保存到指定的路径 60   /// </summary> 61   /// <param name="Width">缩略图的宽度</param> 62   /// <param name="Height">缩略图的高度</param> 63   /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:\Images\filename.jpg</param> 64   /// <returns>成功返回true,否则返回false</returns> 65   public bool GetReducedImage(int Width,int Height,string targetFilePath) 66   { 67   try 68    { 69    Image ReducedImage; 70  71    Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);  72    73    ReducedImage=ResourceImage.GetThumbnailImage(Width,Height,callb,IntPtr.Zero); 74    ReducedImage.Save(@targetFilePath,ImageFormat.Jpeg); 75  76    ReducedImage.Dispose();  77   78    return true; 79    } 80   catch(Exception e) 81    { 82    ErrMessage=e.Message;  83    return false; 84    } 85   } 86  87   /// <summary> 88   /// 生成缩略图重载方法3,返回缩略图的Image对象 89   /// </summary> 90   /// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>  91   /// <returns>缩略图的Image对象</returns> 92   public Image GetReducedImage(double Percent) 93   { 94   try 95    { 96    Image ReducedImage; 97  98    Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback); 99 100    ImageWidth=Convert.ToInt32(ResourceImage.Width*Percent);101    ImageHeight=Convert.ToInt32(ResourceImage.Width*Percent);102   103    ReducedImage=ResourceImage.GetThumbnailImage(ImageWidth,ImageHeight,callb,IntPtr.Zero);104  105    return ReducedImage;106    }107   catch(Exception e)108    {109    ErrMessage=e.Message; 110    return null;111    }112   }113 114   /// <summary>115   /// 生成缩略图重载方法4,返回缩略图的Image对象116   /// </summary>117   /// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param> 118   /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:\Images\filename.jpg</param>119   /// <returns>成功返回true,否则返回false</returns>120   public bool GetReducedImage(double Percent,string targetFilePath)121   {122   try123    {124    Image ReducedImage;125 126    Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);127 128    ImageWidth=Convert.ToInt32(ResourceImage.Width*Percent);129    ImageHeight=Convert.ToInt32(ResourceImage.Width*Percent);130   131    ReducedImage=ResourceImage.GetThumbnailImage(ImageWidth,ImageHeight,callb,IntPtr.Zero);132 133    ReducedImage.Save(@targetFilePath,ImageFormat.Jpeg);134 135    ReducedImage.Dispose(); 136  137    return true;138    }139   catch(Exception e)140    {141    ErrMessage=e.Message; 142    return false;143    }144   }145 }

ThumbnailClass