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

    技术2022-05-11  69

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

     说明:  这个功能我想了很久了...  以前用 ASP 的时候, 我就老想不靠第三方组件生成缩略图, 为的啥...通用...  现在好了, .net 提供了一大堆一大堆的类库, 让我们可以实现很多功能, 比如生成缩略图, 当然还有N多的高级功能, 哈哈, 我的想法挺多的, 这下该我兴奋的地方真是太多太多了..  总之, 从 ASP 升级到 .net 准没错.    shawl.qiu  2007-01-31  http://blog.csdn.net/btbtd    c# .net 生成缩略图类 thumb 源代码 及演示(支持从文件或从流读取内容): <%@ Page Language="C#" AutoEventWireup="True" %> <%@ import Namespace="System.IO"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>shawl.qiu template</title> <script runat="server">  void Page_Load(Object sender, EventArgs e)  {    /*   // overloading file 1   String sImgNm="01.jpg";   String sImg=MapPath("uploads/"+sImgNm);   String sImgThumb=MapPath("uploads/thumb/"+sImgNm);      thumb.file(sImg, sImgThumb, 200, 200); */ /*   // overloading file 2   String sImg=MapPath("uploads/04.jpg");   thumb.file(sImg); */ /*   // overloading file 3   thumb.file("uploads/01.jpg", true); */ /*   // overloading stream 1   String sFlNm="01.jpg";   String sFlPh=MapPath("uploads/"+sFlNm);   String sThPh=MapPath("uploads/thumb/"+sFlNm);      using(FileStream fs=new FileStream(sFlPh, FileMode.Open, FileAccess.Read))   {    thumb.stream(fs, sThPh);   } */ /*   // overloading stream 2   String sFlNm="03.jpg";   String sFlPh=MapPath("uploads/"+sFlNm);   String sThPh="uploads/thumb/"+sFlNm;      using(FileStream fs=new FileStream(sFlPh, FileMode.Open, FileAccess.Read))   {    thumb.stream(fs, sThPh, true);   } */ /*   // overloading stream 3   String sFlNm="04.jpg";   String sFlPh=MapPath("uploads/"+sFlNm);   String sThPh="uploads/thumb/"+sFlNm;      using(FileStream fs=new FileStream(sFlPh, FileMode.Open, FileAccess.Read))   {    thumb.stream(fs, sThPh, 200, 200, true);   } */ /*   // overloading stream 4   String sFlNm="04.jpg";   String sFlPh=MapPath("uploads/"+sFlNm);   String sThPh=MapPath("uploads/thumb/"+sFlNm);      using(FileStream fs=new FileStream(sFlPh, FileMode.Open, FileAccess.Read))   {    thumb.stream(fs, sThPh, 200, 200);   } */    // overloading file 3   //thumb.file("uploads/01.jpg", true);      // overloading file 3   //thumb.file("uploads/01.png", true);      // overloading file 3   thumb.file("uploads/01.gif", true);  } // end Page_Load /*-----------------------------------------------------------------------------------*/  * shawl.qiu c# .net thumb class v1.0 /*-----------------------------------------------------------------------------------*/ //---------------------------------------------------------------------begin class thumb public class thumb {  //-----------------------------------begin event  public thumb()  {  }   ~thumb()  {  }  //-----------------------------------end event   //-----------------------------------begin public constant  //-----------------------begin about  public const String auSubject="shawl.qiu c# .net thumb class";  public const String auVersion="v1.0";  public const String au="shawl.qiu";  public const String auEmail="shawl.qiu@gmail.com";  public const String auBlog="http://blog.csdn.net/btbtd";  public const String auCreateDate="2007-1-31";  //-----------------------end about  //-----------------------------------end public constant   //-----------------------------------begin private constant  //-----------------------------------end private constant   //-----------------------------------begin public static method   //------------------begin stream  public static void stream(Stream oStm, String sThumbPath)  {   saveThumb_stream(oStm, sThumbPath, 200, 200);  }   public static void stream(Stream oStm, String sThumbPath, Boolean bCov)  {   if(bCov)   {    sThumbPath=System.Web.HttpContext.Current.Server.MapPath(sThumbPath);   }   saveThumb_stream(oStm, sThumbPath, 200, 200);  }   public static void stream(Stream oStm, String sThumbPath,    Int32 iWidth, Int32 iHeight, Boolean bCov)  {   if(bCov)   {    sThumbPath=System.Web.HttpContext.Current.Server.MapPath(sThumbPath);   }   saveThumb_stream(oStm, sThumbPath, iWidth, iHeight);  }   public static void stream(Stream oStm, String sThumbPath, Int32 iWidth, Int32 iHeight)  {   saveThumb_stream(oStm, sThumbPath, iWidth, iHeight);  }  //------------------end stream   //------------------begin file  public static void file(String sImgPath, String sThumbPath, Int32 iImgWidth, Int32 iImgHeight)  {   thumb.saveThumb(sImgPath, sThumbPath, iImgWidth, iImgHeight);  } // end public static void file   public static void file(String sImgPath)  {   // 检测同级目录是否存在 thumb 目录   String sCreateThumbPath=System.IO.Path.GetDirectoryName(sImgPath)+"//thumb//";   if(!System.IO.Directory.Exists(sCreateThumbPath))   {    System.IO.Directory.CreateDirectory(sCreateThumbPath);   }      String sThumbPath=sCreateThumbPath+System.IO.Path.GetFileName(sImgPath);   Int32 iImgWidth=200;   Int32 iImgHeight=200;      thumb.saveThumb(sImgPath, sThumbPath, iImgWidth, iImgHeight);     } // end public static void file   public static void file(String sImgPath, Boolean bCov)  {   if(bCov)   {    sImgPath=System.Web.HttpContext.Current.Server.MapPath(sImgPath);   }   thumb.file(sImgPath);  } // end public static void file  //------------------end file  //-----------------------------------end public static method   //-----------------------------------begin private static method   private static void saveThumb_stream(Stream oStm, String sThumbPath,    Int32 iImgWidth, Int32 iImgHeight)  {   using(System.Drawing.Image oImg=System.Drawing.Image.FromStream(oStm))   {    System.Drawing.Image.GetThumbnailImageAbort callback =     new System.Drawing.Image.GetThumbnailImageAbort(thumb.callback);        Int32 iOldWidth=oImg.Width;    Int32 iOldHeight=oImg.Height;        // 按比较设置宽高    thumb.mtSetSize(ref iOldWidth, ref iOldHeight, iImgWidth, iImgHeight);        // 生成并保存缩略图    using(System.Drawing.Image oThumb=     oImg.GetThumbnailImage(iOldWidth, iOldHeight, callback, IntPtr.Zero))    {    oThumb.Save(sThumbPath, System.Drawing.Imaging.ImageFormat.Jpeg);    }   }  } // end private static void saveThumb   private static bool callback()  {   return false;  } // end private static bool callback   private static void mtSetSize(ref Int32 iWidth_, ref Int32 iHeight_,    Int32 iSetWidth, Int32 iSetHeight)  {   Single iWidth=(Single)iWidth_;   Single iHeight=(Single)iHeight_;      if(iWidth>iHeight)   {    if(iWidth>iSetWidth)    {     iHeight=iHeight-(iHeight/(iWidth/(iWidth-iSetWidth)));     iWidth=iSetWidth;    }   }      if(iWidth<iHeight)   {    if(iHeight>iSetHeight)    {     iWidth=iWidth-(iWidth/(iHeight/(iHeight-iSetHeight)));     iHeight=iSetHeight;    }   }      if(iWidth==iHeight)   {    if(iHeight>iSetHeight)    {      iHeight=iSetHeight;     iWidth=iSetWidth;    }   }   iWidth_=(Int32)iWidth;   iHeight_=(Int32)iHeight;  } // end private static void mtSetSize   private static void saveThumb(String sImgPath, String sThumbPath,    Int32 iImgWidth, Int32 iImgHeight)  {   using(System.Drawing.Image oImg=System.Drawing.Image.FromFile(sImgPath))   {    System.Drawing.Image.GetThumbnailImageAbort callback =     new System.Drawing.Image.GetThumbnailImageAbort(thumb.callback);        Int32 iOldWidth=oImg.Width;    Int32 iOldHeight=oImg.Height;        // 按比较设置宽高    thumb.mtSetSize(ref iOldWidth, ref iOldHeight, iImgWidth, iImgHeight);        // 生成并保存缩略图    using(System.Drawing.Image oThumb=     oImg.GetThumbnailImage(iOldWidth, iOldHeight, callback, IntPtr.Zero))    {     //oThumb.Save(sThumbPath, System.Drawing.Imaging.ImageFormat.Jpeg);     oThumb.Save(sThumbPath, oImg.RawFormat);    }   }  } // end private static void saveThumb  //-----------------------------------end private static method   //-----------------------------------begin public variable  //-----------------------------------end public variable   //-----------------------------------begin public method  //-----------------------------------end public method   //-----------------------------------begin private variable  //-----------------------------------end private variable   //-----------------------------------begin private method  //-----------------------------------end private method   //-----------------------------------begin public property  //-----------------------------------end public property     //-----------------------------------begin private property  //-----------------------------------end private property } //---------------------------------------------------------------------end class thumb </script> </head> <body>  <form runat="server">  </form> </body> </html>

    最新回复(0)