c#生成图片缩略图的类

第一种

/** <summary>
       /// 生成缩略图
       /// </summary>
       /// <param name="originalImagePath">源图路径(物理路径)</param>
       /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
       /// <param name="width">缩略图宽度</param>
       /// <param name="height">缩略图高度</param>
       /// <param name="mode">生成缩略图的方式</param>    
       public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
       {
           Image originalImage = Image.FromFile(originalImagePath);
           
           int towidth = width;
           int toheight = height;
       
           int x = 0;
           int y = 0;
           int ow = originalImage.Width;
           int oh = originalImage.Height;        

           switch (mode)
           {        
               case "HW"://指定高宽缩放(可能变形)                
                   break;
               case "W"://指定宽,高按比例                    
                   toheight = originalImage.Height * width/originalImage.Width;
                   break;
               case "H"://指定高,宽按比例
                   towidth = originalImage.Width * height/originalImage.Height;                    
                   break;        
               case "Cut"://指定高宽裁减(不变形)                
                   if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight)
                   {
                       oh = originalImage.Height;
                       ow = originalImage.Height*towidth/toheight;
                       y = 0;
                       x = (originalImage.Width - ow)/2;
                   }
                   else
                   {
                       ow = originalImage.Width;
                       oh = originalImage.Width*height/towidth;
                       x = 0;
                       y = (originalImage.Height - oh)/2;
                   }
                   break;                    
               default :
                   break;
           }    
           
           //新建一个bmp图片
           Image bitmap = new System.Drawing.Bitmap(towidth,toheight);

           //新建一个画板
           Graphics g = System.Drawing.Graphics.FromImage(bitmap);

           //设置高质量插值法
           g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

           //设置高质量,低速度呈现平滑程度
           g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

           //清空画布并以透明背景色填充
           g.Clear(Color.Transparent);        

           //在指定位置并且按指定大小绘制原图片的指定部分
           g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
               new Rectangle(x, y, ow,oh),
               GraphicsUnit.Pixel);

           try
           {            
               //以jpg格式保存缩略图
               bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
           }
           catch(System.Exception e)
           {
               throw e;
           }
           finally
           {
               originalImage.Dispose();
               bitmap.Dispose();                        
               g.Dispose();
           }
       }

关键方法Graphics.DrawImage见ms-help://MS.NETFrameworkSDKv1.1.CHS/cpref/html/frlrfsystemdrawinggraphicsclassdrawimagetopic11.htm

第二种

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 ilename.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 ilename.jpg</param>   
  119.     /// <returns>成功返回true,否则返回false</returns>   
  120.     public bool GetReducedImage(double Percent,string targetFilePath)   
  121.     {   
  122.      try  
  123.      {   
  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.   
  146.   
  147. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值