C#为图片生成缩略图

// 类名:SetThumbnailImages.cs   

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;

namespace MyClass
{
 ///
 ///
按图片比例给图片自动生成缩略图,生成该类调用时,注意首先创建对象,以便初始化应用构建器
 /// @author:
方继祥
 /// @pubdate:2004-12-23
 /// @描述:此类由JAVA生成缩略图演变而来
 ///
 public class SetThumbnailImages
 {
  public SetThumbnailImages()
  {
   //
   // TODO:
在此处添加构造函数逻辑
   //
   #region 
构建器(初始化)
   htmimes[".jpe"]="image/jpeg";
   htmimes[".jpeg"]="image/jpeg";
   htmimes[".jpg"]="image/jpeg";
   htmimes[".png"]="image/png";
   htmimes[".tif"]="image/tiff";
   htmimes[".tiff"]="image/tiff";
   htmimes[".bmp"]="image/bmp";
   #endregion
  }

  public Hashtable htmimes = new Hashtable();
  public string AllowExt = ".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp";
  ///
  ///
获取图像编码解码器的所有相关信息
  ///
  ///
包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串
  ///
返回图像编码解码器的所有相关信息
  public ImageCodecInfo GetCodecInfo(string mimeType)
  {
   ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
   foreach(ImageCodecInfo ici in CodecInfo)
   {
    if(ici.MimeType == mimeType)return ici;
   }
   return null;
  }

  ///
  ///
检测扩展名的有效性
  ///
  ///
文件名扩展名
  ///
如果扩展名有效,返回true,否则返回false.
  public bool CheckValidExt(string sExt)
  {
   bool flag=false;
   string[] aExt = AllowExt.Split('|');
   foreach(string filetype in aExt)
   {
    if(filetype.ToLower()==sExt)
    {
     flag = true;
     break;
    }
   }

 return flag;
  }

  ///
  ///
保存图片
  ///
  /// Image
对象
  ///
保存路径
  ///
指定格式的编解码参数
  public void SaveImage(System.Drawing.Image image,string savePath,ImageCodecInfo ici)
  {
   //
设置原图片对象的 EncoderParameters 对象
   EncoderParameters parameters = new EncoderParameters(1);
   parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ((long) 90));
   image.Save(savePath, ici, parameters);
   parameters.Dispose();
  } 

  ///
  ///
生成缩略图
  ///
  ///
原图片路径(绝对路径)
  ///
生成的缩略图路径,如果为空则保存为原图片路径(绝对路径)
  ///
缩略图的宽度(高度与按源图片比例自动生成)
  public void ToThumbnailImages(string SourceImagePath,string ThumbnailImagePath,int ThumbnailImageWidth)
  {
   //
取得扩展名
   string sExt = SourceImagePath.Substring(SourceImagePath.LastIndexOf(".")).ToLower();
   if(SourceImagePath.ToString()==System.String.Empty) throw new NullReferenceException("SourceImagePath is null!");
   if(!CheckValidExt(sExt))
   {
    throw new ArgumentException("
原图片文件格式不正确,支持的格式有[ "+ AllowExt +" ]",SourceImagePath);
   }
   //
从原图片创建Image 对象
   System.Drawing.Image image = System.Drawing.Image.FromFile(SourceImagePath);
   int num = ((ThumbnailImageWidth / 4) * 3);
   int width = image.Width;
   int height = image.Height;
   //
计算图片的比例
   if ((((double) width) / ((double) height)) >= 1.3333333333333333f)
   {
    num = ((height * ThumbnailImageWidth) / width);
   }
   else
   {
    ThumbnailImageWidth = ((width * num) / height);
   }
   if ((ThumbnailImageWidth < 1) || (num < 1))
   {
    return;
   }
   //
用指定的大小和格式初始化 Bitmap 类的新实例
   Bitmap bitmap = new Bitmap(ThumbnailImageWidth, num, PixelFormat.Format32bppArgb);
   //
从指定的 Image 对象创建新 Graphics 对象
   Graphics graphics = Graphics.FromImage(bitmap);
   //
清除整个绘图面并以透明背景色填充
   graphics.Clear(Color.Transparent);
   //
在指定位置并且按指定大小绘制原图片对象
   graphics.DrawImage(image, new Rectangle(0, 0, ThumbnailImageWidth, num));
   image.Dispose();
   try
   {
    //
将此原图片以指定格式并用指定的编解码参数保存到指定文件
    string savepath = (ThumbnailImagePath==null?SourceImagePath:ThumbnailImagePath);
    SaveImage(bitmap,savepath,GetCodecInfo((string)htmimes[sExt]));
    
   }
   catch(System.Exception e)
   {
    throw e;
   }
   finally
   {
    bitmap.Dispose();
    graphics.Dispose();
   }
  }
 }
}

//调用时

string oldPath=Server.Mappath(“/image/1.jpg“);
string newPath = Server.Mappath(“/image/s_1.jpg..“);
//
创建对象初始化
Myclass.SetThumbnailImages setSmall = new Myclass.SetThumbnailImages();
setSmall.ToThumbnailImages(oldPath,newPath,300);

posted @ 2016-05-17 10:50  lovemx  阅读(329)  评论(0)    收藏  举报