/**//// <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(); 
           } 
       } 

第二种

4个重载方法,有直接返回Image对象的,有生成缩略图,并且保存到指定目录的!


using System.IO;
using System.Drawing;
using System.Drawing.Imaging; /// <summary>
/// 图片处理类
/// 1、生成缩略图片或按照比例改变图片的大小和画质
/// 2、将生成的缩略图放到指定的目录下
/// </summary>
public class ImageClass
{
    public Image ResourceImage;
    private int ImageWidth;
    private int ImageHeight;     public string ErrMessage;     /// <summary>
    /// 类的构造函数
    /// </summary>
    /// <param name="ImageFileName">图片文件的全路径名称</param>
    public ImageClass(string ImageFileName)
    {
        ResourceImage=Image.FromFile(ImageFileName);
     ErrMessage="";
    }     public bool ThumbnailCallback()
    {
     return false;
    }     /// <summary>
    /// 生成缩略图重载方法1,返回缩略图的Image对象
    /// </summary>
    /// <param name="Width">缩略图的宽度</param>
    /// <param name="Height">缩略图的高度</param>
    /// <returns>缩略图的Image对象</returns>
    public Image GetReducedImage(int Width,int Height)
    {
     try
     {
      Image ReducedImage;       Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback); 
     
      ReducedImage=ResourceImage.GetThumbnailImage(Width,Height,callb,IntPtr.Zero);
  
      return ReducedImage;
     }
     catch(Exception e)
     {
      ErrMessage=e.Message; 
         return null;
     }
    }     /// <summary>
    /// 生成缩略图重载方法2,将缩略图文件保存到指定的路径
    /// </summary>
    /// <param name="Width">缩略图的宽度</param>
    /// <param name="Height">缩略图的高度</param>
    /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:\Images\filename.jpg</param>
    /// <returns>成功返回true,否则返回false</returns>
    public bool GetReducedImage(int Width,int Height,string targetFilePath)
    {
     try
     {
      Image ReducedImage;       Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback); 
     
      ReducedImage=ResourceImage.GetThumbnailImage(Width,Height,callb,IntPtr.Zero);
      ReducedImage.Save(@targetFilePath,ImageFormat.Jpeg);       ReducedImage.Dispose(); 
  
      return true;
     }
     catch(Exception e)
     {
      ErrMessage=e.Message; 
      return false;
     }
    }     /// <summary>
    /// 生成缩略图重载方法3,返回缩略图的Image对象
    /// </summary>
    /// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>  
    /// <returns>缩略图的Image对象</returns>
    public Image GetReducedImage(double Percent)
    {
     try
     {
      Image ReducedImage;       Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);       ImageWidth=Convert.ToInt32(ResourceImage.Width*Percent);
      ImageHeight=Convert.ToInt32(ResourceImage.Width*Percent);
     
      ReducedImage=ResourceImage.GetThumbnailImage(ImageWidth,ImageHeight,callb,IntPtr.Zero);
  
      return ReducedImage;
     }
     catch(Exception e)
     {
      ErrMessage=e.Message; 
      return null;
     }
    }     /// <summary>
    /// 生成缩略图重载方法4,返回缩略图的Image对象
    /// </summary>
    /// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>  
    /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:\Images\filename.jpg</param>
    /// <returns>成功返回true,否则返回false</returns>
    public bool GetReducedImage(double Percent,string targetFilePath)
    {
     try
     {
      Image ReducedImage;       Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback);       ImageWidth=Convert.ToInt32(ResourceImage.Width*Percent);
      ImageHeight=Convert.ToInt32(ResourceImage.Width*Percent);
     
      ReducedImage=ResourceImage.GetThumbnailImage(ImageWidth,ImageHeight,callb,IntPtr.Zero);       ReducedImage.Save(@targetFilePath,ImageFormat.Jpeg);       ReducedImage.Dispose(); 
  
      return true;
     }
     catch(Exception e)
     {
      ErrMessage=e.Message; 
      return false;
     }
    } }
 

C#生成缩略图代码的更多相关文章

  1. asp.net中生成缩略图并添加版权实例代码

    这篇文章介绍了asp.net中生成缩略图并添加版权实例代码,有需要的朋友可以参考一下 复制代码代码如下: //定义image类的对象 Drawing.Image image,newimage; //图 ...

  2. ThinkPHP5.0图片上传生成缩略图实例代码

    很多朋友遇到这样一个问题,图片上传生成缩略图,很多人在本机(win)测试成功,上传到linux 服务器后错误. 我也遇到同样的问题.网上一查,有无数的人说是服务器临时文件目录权限问题. 几经思考后,发 ...

  3. ASP.NET生成缩略图的代码

    01.        // <summary> 02.        /// 生成缩略图 03.        /// </summary> 04.        /// &l ...

  4. C#中图片切割,图片压缩,缩略图生成的代码

    **//// <summary> /// 图片切割函数 /// </summary> /// <param name="sourceFile"> ...

  5. phpcms v9图片生成缩略图变成黑色解决方法

    今天客户反映,上传的图片生成缩略图有的图片变成黑色,出现问题就百度了一下,有不少网友也遇到这样的问题,但是官方论坛也没有给出解决办法,那还得靠自己解决了,于是就研究phpcms v9 图片压缩代码.打 ...

  6. js无刷新上传图片,服务端有生成缩略图,剪切图片,iphone图片旋转判断功能

    html: <form action="<{:AppLink('circle/uploadimg')}>" id="imageform" me ...

  7. .net又一个生成缩略图的方法,不变形

    生成缩略图是一个十分常用功能,找到了一个方法,重写部分代码,实用又好用,.net又一个生成缩略图的方法,不变形 /// <summary> /// 为图片生成缩略图 by 何问起 /// ...

  8. yii php 图片上传与生成缩略图

    今天需要做图片上传与生成缩略图的功能,把代码进行记录如下: html 视图              ($pic_action_url = $this->createAbsoluteUrl('h ...

  9. 使用Uploadify实现上传图片生成缩略图例子,实时显示进度条

    不了解Uploadify的,先看看前一篇详细说明 http://www.cnblogs.com/XuebinDing/archive/2012/04/26/2470995.html Uploadify ...

随机推荐

  1. centos6.5安装配置LDAP服务[转]

    安装之前查一下 1 find / -name openldap* centos6.4默认安装了LDAP,但没有装ldap-server和ldap-client 于是yum安装 1 su root 2 ...

  2. SQL 跨服务器数据库增、删、改、查(一)

    --开启本服务器中操作其他服务器的功能 reconfigure --输出消息 reconfigure --输出消息 --增 INSERT INTO OPENROWSET('SQLOLEDB','jx3 ...

  3. Contest2037 - CSU Monthly 2013 Oct(中南大学2013年10月月赛水题部分题解)

    Problem A: Small change 题解:http://www.cnblogs.com/crazyapple/p/3349469.html Problem B: Scoop water 题 ...

  4. 相似元素存在的意义---HTML&CSS

    1.<q> 效果: 告诉浏览器这是一段短引用,让浏览器以合适的方法来显示 注: 不能直接以双引号直接代替<q>,因为有些浏览器<q>的效果不是双引号. 不要忘了移动 ...

  5. 1185: [HNOI2007]最小矩形覆盖 - BZOJ

      就是一道凸包(枚举凸包的边作为矩形的一条边)的裸题,只是不太好打,所以犹豫到今天才打 不说了,说起AC都是泪啊,因为没有精度判断,没有判重(算距离时除0了)错了好久 拍了好久都和标称是一样的,因为 ...

  6. 1176: [Balkan2007]Mokia - BZOJ

    Description维护一个W*W的矩阵,每次操作可以增加某格子的权值,或询问某子矩阵的总权值. 修改操作数M<=160000,询问数Q<=10000,W<=2000000.Inp ...

  7. 基于局部敏感哈希的协同过滤算法之simHash算法

    搜集了快一个月的资料,虽然不完全懂,但还是先慢慢写着吧,说不定就有思路了呢. 开源的最大好处是会让作者对脏乱臭的代码有羞耻感. 当一个做推荐系统的部门开始重视[数据清理,数据标柱,效果评测,数据统计, ...

  8. [转载]MongoDB设置访问权限、设置用户

    MongoDB已经使用很长一段时间了,基于MongoDB的数据存储也一直没有使用到权限访问(MongoDB默认设置为无权限访问限制),今天特地花了一点时间研究了一下,研究成果如下: 注:研究成果基于W ...

  9. [转载]js 遍历数组对象

    有一个JSON数组如下 all = {"error":0,"content":[{"name":"北京","v ...

  10. 从List[Future[T]]到Future[List[T]]

    在课程<Principles Of Reactive Programming>里Week3的一节 "Promises, promises, promises"中,Eri ...