分享

用C#实现图片的缩放

 万子千秋 2010-10-26
  1. class ExampleOfLegalsoft   
  2. {   
  3.      //************************************************************//   
  4.      //下面给出三个简单的方法,后面两个方法是扩展,估计有时用得着   
  5.      //************************************************************//   
  6.      /// <summary>   
  7.      /// 缩小图片   
  8.      /// </summary>   
  9.      /// <param name="strOldPic">源图文件名(包括路径)</param>   
  10.      /// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>   
  11.      /// <param name="intWidth">缩小至宽度</param>   
  12.      /// <param name="intHeight">缩小至高度</param>   
  13.      public void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)   
  14.      {   
  15.           System.Drawing.Bitmap objPic, objNewPic;   
  16.           try  
  17.           {   
  18.                 objPic = new System.Drawing.Bitmap(strOldPic);   
  19.                 objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);   
  20.                 objNewPic.Save(strNewPic);   
  21.           }   
  22.           catch (Exception exp) { throw exp; }   
  23.           finally  
  24.           {   
  25.                 objPic = null;   
  26.                 objNewPic = null;   
  27.           }   
  28.      }   
  29.      /// <summary>   
  30.      /// 按比例缩小图片,自动计算高度   
  31.      /// </summary>   
  32.      /// <param name="strOldPic">源图文件名(包括路径)</param>   
  33.      /// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>   
  34.      /// <param name="intWidth">缩小至宽度</param>   
  35.      public void SmallPic(string strOldPic, string strNewPic, int intWidth)   
  36.      {   
  37.           System.Drawing.Bitmap objPic, objNewPic;   
  38.           try  
  39.           {   
  40.                 objPic = new System.Drawing.Bitmap(strOldPic);   
  41.                 int intHeight = (intWidth / objPic.Width) * objPic.Height;   
  42.                 objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);   
  43.                 objNewPic.Save(strNewPic);   
  44.           }   
  45.           catch (Exception exp) { throw exp; }   
  46.           finally  
  47.           {   
  48.                 objPic = null;   
  49.                 objNewPic = null;   
  50.           }   
  51.      }   
  52.      /// <summary>   
  53.      /// 按比例缩小图片,自动计算宽度   
  54.      /// </summary>   
  55.      /// <param name="strOldPic">源图文件名(包括路径)</param>   
  56.      /// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>   
  57.      /// <param name="intHeight">缩小至高度</param>   
  58.      public void SmallPic(string strOldPic, string strNewPic, int intHeight)   
  59.      {   
  60.           System.Drawing.Bitmap objPic, objNewPic;   
  61.           try  
  62.           {   
  63.                 objPic = new System.Drawing.Bitmap(strOldPic);   
  64.                 int intWidth = (intHeight / objPic.Height) * objPic.Width;   
  65.                 objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);   
  66.                 objNewPic.Save(strNewPic);   
  67.           }   
  68.           catch (Exception exp) { throw exp; }   
  69.           finally  
  70.           {   
  71.                 objPic = null;   
  72.                 objNewPic = null;   
  73.           }   
  74.      }   
  75. }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多