如何在C#中實現圖片縮放

openkk 12年前發布 | 2K 次閱讀 WP 8.1

 /// <summary>
  /// 縮小圖片
  /// </summary>
  /// <param name="strOldPic">源圖文件名(包括路徑)</param>
  /// <param name="strNewPic">縮小后保存為文件名(包括路徑)</param>
  /// <param name="intWidth">縮小至寬度</param>
  /// <param name="intHeight">縮小至高度</param>
  public void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)
  {

System.Drawing.Bitmap objPic,objNewPic; try { objPic = new System.Drawing.Bitmap(strOldPic); objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight); objNewPic.Save(strNewPic);

} catch(Exception exp){throw exp;} finally { objPic=null; objNewPic=null; } }

/// <summary> /// 按比例縮小圖片,自動計算高度 /// </summary> /// <param name="strOldPic">源圖文件名(包括路徑)</param> /// <param name="strNewPic">縮小后保存為文件名(包括路徑)</param> /// <param name="intWidth">縮小至寬度</param> public void SmallPic(string strOldPic, string strNewPic, int intWidth) {

System.Drawing.Bitmap objPic,objNewPic; try { objPic = new System.Drawing.Bitmap(strOldPic); int intHeight=(intWidth / objPic.Width) * objPic.Height; objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight); objNewPic.Save(strNewPic);

} catch(Exception exp){throw exp;} finally { objPic=null; objNewPic=null; } }

/// <summary> /// 按比例縮小圖片,自動計算寬度 /// </summary> /// <param name="strOldPic">源圖文件名(包括路徑)</param> /// <param name="strNewPic">縮小后保存為文件名(包括路徑)</param> /// <param name="intHeight">縮小至高度</param> public void SmallPic(string strOldPic, string strNewPic, int intHeight) {

System.Drawing.Bitmap objPic,objNewPic; try { objPic = new System.Drawing.Bitmap(strOldPic); int intWidth=(intHeight / objPic.Height) * objPic.Width; objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight); objNewPic.Save(strNewPic);

} catch(Exception exp){throw exp;} finally { objPic=null; objNewPic=null; } }</pre>

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!