如何创建上传文件的缩图?

来源:互联网 发布:windows xp vl 编辑:程序博客网 时间:2024/05/16 17:32
     在下述程式代码中,函数MakeThumbImage(string sPath, string stPath, int nWidth, int nHeight, string sMode)创建上传文件的缩略图。其中,参数sPath表示源图路径、参数stPath表示缩略图的路径、参数nWidth表示缩略图的宽度、参数nHeight表示缩略图的高度、参数sMode表示生成缩略图的方式。
该函数在创建缩略图之前,首先获取源图,并构建源图的Image类的实例sImage,然后根据参数sMode,即创建缩图的模式,计算出缩图的高度和宽度。该函数处理了4种模式:
    HW:指定图片的高度和宽度进行缩放;
    H:指定图片的高度进行缩放;
    W:指定图片的宽度进行缩放;
    Cut:按照图片的原始比例,对图片对行缩放。
  1.     /// <summary>
  2.     /// 生成缩图
  3.     /// </summary>
  4.     /// <param name="sPath">源图路径</param>
  5.     /// <param name="stPath">缩略图的路径</param>
  6.     /// <param name="nWidth">缩略图的宽度</param>
  7.     /// <param name="nHeight">缩略图的高度</param>
  8.     /// <param name="sMode">生成缩略图的方式(HW:指定图片的高度和宽度进行缩放;  H:指定图片的高度进行缩放;  W:指定图片的宽图进行缩放;   Cut:按照图片的源始比例,对图片对行缩放)</param>
  9.     private void MakeThumbImage(string sPath, string stPath, int nWidth, int nHeight, string sMode)
  10.     {
  11.         System.Drawing.Image sImage = System.Drawing.Image.FromFile(sPath);
  12.         int tw = nWidth;
  13.         int th = nHeight;
  14.         // 原始图片的宽度和高度
  15.         int sw = sImage.Width;
  16.         int sh = sImage.Height;
  17.         int x = 0, y = 0;
  18.         switch (sMode)
  19.         {
  20.             case "HW":    // 指定高宽缩放
  21.                 break;
  22.             case "W":    // 指定图片的宽度,计算图片的高度
  23.                 th = sImage.Height * nWidth / sImage.Width;
  24.                 break;
  25.             case "H":    // 指明定图片的高度, 计算图片的宽度
  26.                 sw = sImage.Width * nHeight / sImage.Height;
  27.                 break;
  28.             case "Cut":    // 计算略图的大小
  29.                 if ((double)tw / (double)th < (double)nWidth / (double)nHeight)
  30.                 {
  31.                     sw = sImage.Width;
  32.                     sh = sImage.Width * nHeight / tw;
  33.                     x = 0;
  34.                     y = (sImage.Height - sh) / 2;
  35.                 }
  36.                 else
  37.                 {
  38.                     sh = sImage.Height;
  39.                     sw = sImage.Height * tw / th;
  40.                     y = 0;
  41.                     x = (sImage.Width - sw) / 2;
  42.                 }
  43.                 break;
  44.             default:
  45.                 break;
  46.         }
  47.         // 创建bmp图片
  48.         System.Drawing.Image bitmap = new System.Drawing.Bitmap(tw, th);
  49.         // 创建Graphics对象g
  50.         System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
  51.         // 设置高质量插值法
  52.         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  53.         // 设置高质量,低速度呈现平滑程度
  54.         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  55.         // 清空画布并以透明背景色填充
  56.         g.Clear(System.Drawing.Color.Transparent);
  57.         // 在指定位置并且按指定大小绘制原图片的指定部份
  58.         g.DrawImage(sImage, new System.Drawing.Rectangle(0, 0, tw, th), new System.Drawing.Rectangle(x, y, sw, sh), System.Drawing.GraphicsUnit.Pixel);
  59.         try
  60.         {
  61.             // 采用jpg格式保存缩略图
  62.             bitmap.Save(stPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  63.         }
  64.         catch (Exception ex)
  65.         {
  66.             Response.Write(ex.Message);
  67.         }
  68.         finally
  69.         {
  70.             // 释放资源
  71.             sImage.Dispose();
  72.             bitmap.Dispose();
  73.             g.Dispose();
  74.         }
  75.     }