生成缩略图

  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: Edwin
  4.  * Date: 2008-12-18
  5.  * Time: 10:02
  6.  */
  7. using System;
  8. using System.Drawing;
  9. using System.Drawing.Drawing2D;
  10. using System.Drawing.Imaging;
  11. namespace Edwin.Graphic
  12. {
  13.     /// <summary>
  14.     /// 产生缩略图的模式.
  15.     /// </summary>
  16.     public enum GraphicMode
  17.     {   //着重宽度模式
  18.         EmphasizedWidth = 1,
  19.         //着重高度模式
  20.         EmphasizedHeight= 2,
  21.         //切割模式
  22.         Cut=3,
  23.         //指定模式
  24.         Appointed=4
  25.     }
  26.     /// <summary>
  27.     /// 主要用来产生缩略图.
  28.     /// </summary>
  29.     public class GraphicUtil
  30.     {
  31.         public GraphicUtil()
  32.         {
  33.         }
  34.         public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, GraphicMode mode)
  35.         {
  36.             //----------------------------添加参数校验区-------------------------------//
  37.             //如果需要可以添加识别文件是否存在的方法。(采用正则判断格式和文件流判断存在即可)
  38.             //如果添加,反倒不适合批处理,如下的判断也可删除,全部在外围实在判断.
  39.             if(originalImagePath!=null&&thumbnailPath!=null)
  40.             {                   
  41.                 //防止文件同名
  42.                 if(originalImagePath.ToLower().Equals(thumbnailPath.ToLower()))
  43.                 {
  44.                     return;
  45.                 }
  46.             
  47.             }else
  48.             {
  49.                return;
  50.             }
  51.             //------------------------------END------------------------------------------//
  52.             int makeMode = (int)mode;
  53.             Image image = Image.FromFile(originalImagePath);
  54.             int appointedWidth = width;
  55.             int appointedHeight = height;
  56.             int x = 0;
  57.             int y = 0;
  58.             int originalWidth = image.Width;
  59.             int originalHeight = image.Height;
  60.           //----------------------------模式转化区-------------------------------//
  61.             switch (makeMode)
  62.             {
  63.                 case 1:
  64.                     appointedWidth = (image.Width * height) / image.Height;;
  65.                     break;
  66.                 case 2:
  67.                     appointedHeight = (image.Height * width) / image.Width;
  68.                     break;
  69.                 case 3:
  70.                     if ((((double) image.Width) / ((double) image.Height)) > (((double) appointedWidth) / ((double) appointedHeight)))
  71.                     {
  72.                         originalHeight = image.Height;
  73.                         originalWidth = (image.Height * appointedWidth) / appointedHeight;
  74.                         y = 0;
  75.                         x = (image.Width - originalWidth) / 2;
  76.                     }
  77.                     else
  78.                     {
  79.                         originalWidth = image.Width;
  80.                         originalHeight = (image.Width * height) / appointedWidth;
  81.                         x = 0;
  82.                         y = (image.Height - originalHeight) / 2;
  83.                     }
  84.                     break;
  85.                 default:
  86.                     break;
  87.             }
  88.             //------------------------------END------------------------------------------//
  89.             //----------------------------缩略图生成区-------------------------------//
  90.             Image imageNew = new Bitmap(appointedWidth, appointedHeight);
  91.             Graphics graphics = Graphics.FromImage(imageNew);
  92.             graphics.InterpolationMode = InterpolationMode.High;
  93.             graphics.SmoothingMode =SmoothingMode.HighQuality;
  94.             graphics.Clear(Color.Transparent);
  95.             graphics.DrawImage(image, new Rectangle(0, 0, appointedWidth, appointedHeight), new Rectangle(x, y, originalWidth, originalHeight), GraphicsUnit.Pixel);
  96.             try
  97.             {
  98.                 imageNew.Save(thumbnailPath, ImageFormat.Jpeg);
  99.             }
  100.             catch (Exception exception)
  101.             {
  102.                 throw exception;
  103.             }
  104.             finally
  105.             {
  106.                 image.Dispose();
  107.                 imageNew.Dispose();
  108.                 graphics.Dispose();
  109.             }
  110.             //------------------------------END------------------------------------------//
  111.         }
  112.     }
  113. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值