c# .net 生成缩略图(按比例) By shawl.qiu

c# .net 生成缩略图(按比例) By shawl.qiu 

 说明:
 这个功能我想了很久了...
 以前用 ASP 的时候, 我就老想不靠第三方组件生成缩略图, 为的啥...通用...
 现在好了, .net 提供了一大堆一大堆的类库, 让我们可以实现很多功能, 比如生成缩略图, 当然还有N多的高级功能, 哈哈, 我的想法挺多的, 这下该我兴奋的地方真是太多太多了..
 总之, 从 ASP 升级到 .net 准没错.
 
 shawl.qiu
 2007-01-31
 http://blog.csdn.net/btbtd
 
 c# .net 生成缩略图类 thumb 源代码 及演示(支持从文件或从流读取内容):
  1. <%@ Page Language="C#" AutoEventWireup="True" %>
  2. <%@ import Namespace="System.IO"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <title>shawl.qiu template</title>
  8. <script runat="server">
  9.  void Page_Load(Object sender, EventArgs e)
  10.  {
  11.   
  12. /*
  13.   // overloading file 1
  14.   String sImgNm="01.jpg";
  15.   String sImg=MapPath("uploads/"+sImgNm);
  16.   String sImgThumb=MapPath("uploads/thumb/"+sImgNm);
  17.   
  18.   thumb.file(sImg, sImgThumb, 200, 200);
  19. */

  20. /*
  21.   // overloading file 2
  22.   String sImg=MapPath("uploads/04.jpg");

  23.   thumb.file(sImg);
  24. */

  25. /*
  26.   // overloading file 3
  27.   thumb.file("uploads/01.jpg", true);
  28. */

  29. /*
  30.   // overloading stream 1
  31.   String sFlNm="01.jpg";
  32.   String sFlPh=MapPath("uploads/"+sFlNm);
  33.   String sThPh=MapPath("uploads/thumb/"+sFlNm);
  34.   
  35.   using(FileStream fs=new FileStream(sFlPh, FileMode.Open, FileAccess.Read))
  36.   {
  37.    thumb.stream(fs, sThPh);
  38.   }
  39. */
  40. /*
  41.   // overloading stream 2
  42.   String sFlNm="03.jpg";
  43.   String sFlPh=MapPath("uploads/"+sFlNm);
  44.   String sThPh="uploads/thumb/"+sFlNm;
  45.   
  46.   using(FileStream fs=new FileStream(sFlPh, FileMode.Open, FileAccess.Read))
  47.   {
  48.    thumb.stream(fs, sThPh, true);
  49.   }
  50. */
  51. /*
  52.   // overloading stream 3
  53.   String sFlNm="04.jpg";
  54.   String sFlPh=MapPath("uploads/"+sFlNm);
  55.   String sThPh="uploads/thumb/"+sFlNm;
  56.   
  57.   using(FileStream fs=new FileStream(sFlPh, FileMode.Open, FileAccess.Read))
  58.   {
  59.    thumb.stream(fs, sThPh, 200, 200, true);
  60.   }
  61. */

  62. /*
  63.   // overloading stream 4
  64.   String sFlNm="04.jpg";
  65.   String sFlPh=MapPath("uploads/"+sFlNm);
  66.   String sThPh=MapPath("uploads/thumb/"+sFlNm);
  67.   
  68.   using(FileStream fs=new FileStream(sFlPh, FileMode.Open, FileAccess.Read))
  69.   {
  70.    thumb.stream(fs, sThPh, 200, 200);
  71.   }
  72. */
  73.  
  74.   // overloading file 3
  75.   //thumb.file("uploads/01.jpg", true);
  76.   
  77.   // overloading file 3
  78.   //thumb.file("uploads/01.png", true);
  79.   
  80.   // overloading file 3
  81.   thumb.file("uploads/01.gif", true);
  82.  } // end Page_Load

  83. /*-----------------------------------------------------------------------------------*/
  84.  * shawl.qiu c# .net thumb class v1.0
  85. /*-----------------------------------------------------------------------------------*/
  86. //---------------------------------------------------------------------begin class thumb
  87. public class thumb
  88. {
  89.  //-----------------------------------begin event
  90.  public thumb()
  91.  {
  92.  }
  93.  
  94.  ~thumb()
  95.  {
  96.  }
  97.  //-----------------------------------end event
  98.  
  99.  //-----------------------------------begin public constant
  100.  //-----------------------begin about
  101.  public const String auSubject="shawl.qiu c# .net thumb class";
  102.  public const String auVersion="v1.0";
  103.  public const String au="shawl.qiu";
  104.  public const String auEmail="shawl.qiu@gmail.com";
  105.  public const String auBlog="http://blog.csdn.net/btbtd";
  106.  public const String auCreateDate="2007-1-31";
  107.  //-----------------------end about
  108.  //-----------------------------------end public constant
  109.  
  110.  //-----------------------------------begin private constant
  111.  //-----------------------------------end private constant
  112.  
  113.  //-----------------------------------begin public static method
  114.  
  115.  //------------------begin stream
  116.  public static void stream(Stream oStm, String sThumbPath)
  117.  {
  118.   saveThumb_stream(oStm, sThumbPath, 200, 200);
  119.  }
  120.  
  121.  public static void stream(Stream oStm, String sThumbPath, Boolean bCov)
  122.  {
  123.   if(bCov)
  124.   {
  125.    sThumbPath=System.Web.HttpContext.Current.Server.MapPath(sThumbPath);
  126.   }
  127.   saveThumb_stream(oStm, sThumbPath, 200, 200);
  128.  }
  129.  
  130.  public static void stream(Stream oStm, String sThumbPath, 
  131.   Int32 iWidth, Int32 iHeight, Boolean bCov)
  132.  {
  133.   if(bCov)
  134.   {
  135.    sThumbPath=System.Web.HttpContext.Current.Server.MapPath(sThumbPath);
  136.   }
  137.   saveThumb_stream(oStm, sThumbPath, iWidth, iHeight);
  138.  }
  139.  
  140.  public static void stream(Stream oStm, String sThumbPath, Int32 iWidth, Int32 iHeight)
  141.  {
  142.   saveThumb_stream(oStm, sThumbPath, iWidth, iHeight);
  143.  }
  144.  //------------------end stream
  145.  
  146.  //------------------begin file
  147.  public static void file(String sImgPath, String sThumbPath, Int32 iImgWidth, Int32 iImgHeight)
  148.  {
  149.   thumb.saveThumb(sImgPath, sThumbPath, iImgWidth, iImgHeight);
  150.  } // end public static void file
  151.  
  152.  public static void file(String sImgPath)
  153.  {
  154.   // 检测同级目录是否存在 thumb 目录
  155.   String sCreateThumbPath=System.IO.Path.GetDirectoryName(sImgPath)+"//thumb//";
  156.   if(!System.IO.Directory.Exists(sCreateThumbPath))
  157.   {
  158.    System.IO.Directory.CreateDirectory(sCreateThumbPath);
  159.   }
  160.   
  161.   String sThumbPath=sCreateThumbPath+System.IO.Path.GetFileName(sImgPath);
  162.   Int32 iImgWidth=200;
  163.   Int32 iImgHeight=200;
  164.   
  165.   thumb.saveThumb(sImgPath, sThumbPath, iImgWidth, iImgHeight);
  166.   
  167.  } // end public static void file
  168.  
  169.  public static void file(String sImgPath, Boolean bCov)
  170.  {
  171.   if(bCov)
  172.   {
  173.    sImgPath=System.Web.HttpContext.Current.Server.MapPath(sImgPath);
  174.   }
  175.   thumb.file(sImgPath);
  176.  } // end public static void file
  177.  //------------------end file
  178.  //-----------------------------------end public static method
  179.  
  180.  //-----------------------------------begin private static method
  181.  
  182.  private static void saveThumb_stream(Stream oStm, String sThumbPath, 
  183.   Int32 iImgWidth, Int32 iImgHeight)
  184.  {
  185.   using(System.Drawing.Image oImg=System.Drawing.Image.FromStream(oStm))
  186.   {
  187.    System.Drawing.Image.GetThumbnailImageAbort callback =
  188.     new System.Drawing.Image.GetThumbnailImageAbort(thumb.callback);
  189.    
  190.    Int32 iOldWidth=oImg.Width;
  191.    Int32 iOldHeight=oImg.Height;
  192.    
  193.    // 按比较设置宽高
  194.    thumb.mtSetSize(ref iOldWidth, ref iOldHeight, iImgWidth, iImgHeight);
  195.    
  196.    // 生成并保存缩略图
  197.    using(System.Drawing.Image oThumb=
  198.     oImg.GetThumbnailImage(iOldWidth, iOldHeight, callback, IntPtr.Zero))
  199.    {
  200.    oThumb.Save(sThumbPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  201.    }
  202.   }
  203.  } // end private static void saveThumb
  204.  
  205.  private static bool callback()
  206.  {
  207.   return false;
  208.  } // end private static bool callback
  209.  
  210.  private static void mtSetSize(ref Int32 iWidth_, ref Int32 iHeight_, 
  211.   Int32 iSetWidth, Int32 iSetHeight)
  212.  {
  213.   Single iWidth=(Single)iWidth_;
  214.   Single iHeight=(Single)iHeight_;
  215.   
  216.   if(iWidth>iHeight)
  217.   {
  218.    if(iWidth>iSetWidth)
  219.    {
  220.     iHeight=iHeight-(iHeight/(iWidth/(iWidth-iSetWidth)));
  221.     iWidth=iSetWidth;
  222.    }
  223.   }
  224.   
  225.   if(iWidth<iHeight)
  226.   {
  227.    if(iHeight>iSetHeight)
  228.    {
  229.     iWidth=iWidth-(iWidth/(iHeight/(iHeight-iSetHeight)));
  230.     iHeight=iSetHeight;
  231.    }
  232.   }
  233.   
  234.   if(iWidth==iHeight)
  235.   {
  236.    if(iHeight>iSetHeight)
  237.    { 
  238.     iHeight=iSetHeight;
  239.     iWidth=iSetWidth;
  240.    }
  241.   }
  242.   iWidth_=(Int32)iWidth;
  243.   iHeight_=(Int32)iHeight;
  244.  } // end private static void mtSetSize
  245.  
  246.  private static void saveThumb(String sImgPath, String sThumbPath, 
  247.   Int32 iImgWidth, Int32 iImgHeight)
  248.  {
  249.   using(System.Drawing.Image oImg=System.Drawing.Image.FromFile(sImgPath))
  250.   {
  251.    System.Drawing.Image.GetThumbnailImageAbort callback =
  252.     new System.Drawing.Image.GetThumbnailImageAbort(thumb.callback);
  253.    
  254.    Int32 iOldWidth=oImg.Width;
  255.    Int32 iOldHeight=oImg.Height;
  256.    
  257.    // 按比较设置宽高
  258.    thumb.mtSetSize(ref iOldWidth, ref iOldHeight, iImgWidth, iImgHeight);
  259.    
  260.    // 生成并保存缩略图
  261.    using(System.Drawing.Image oThumb=
  262.     oImg.GetThumbnailImage(iOldWidth, iOldHeight, callback, IntPtr.Zero))
  263.    {
  264.     //oThumb.Save(sThumbPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  265.     oThumb.Save(sThumbPath, oImg.RawFormat);
  266.    }
  267.   }
  268.  } // end private static void saveThumb
  269.  //-----------------------------------end private static method
  270.  
  271.  //-----------------------------------begin public variable
  272.  //-----------------------------------end public variable
  273.  
  274.  //-----------------------------------begin public method
  275.  //-----------------------------------end public method
  276.  
  277.  //-----------------------------------begin private variable
  278.  //-----------------------------------end private variable
  279.  
  280.  //-----------------------------------begin private method
  281.  //-----------------------------------end private method
  282.  
  283.  //-----------------------------------begin public property
  284.  //-----------------------------------end public property
  285.   
  286.  //-----------------------------------begin private property
  287.  //-----------------------------------end private property
  288. }
  289. //---------------------------------------------------------------------end class thumb
  290. </script>
  291. </head>
  292. <body>
  293.  <form runat="server">

  294.  </form>
  295. </body>
  296. </html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值