asp.net上传图片文件自动修改图片大小代码
生活随笔
收集整理的這篇文章主要介紹了
asp.net上传图片文件自动修改图片大小代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#region 圖片縮放/// <summary>/// 圖片縮放/// </summary>/// <param name="savePath">圖片相對路徑</param>/// <param name="fileName">圖片名稱</param>/// <param name="destWidth">縮放寬度</param>/// <param name="destHeight">高度</param>/// <param name="type">1--固定縮放;2--按比例縮放;3--指定寬度,寬度大于指定寬度按指定寬度進行等比縮放,小于指定寬度按原圖大小上傳;4--原圖直接上傳</param>/// <returns></returns>public static void ReducesPic(string savePath,string fileName, int destWidth, int destHeight, int type){if (!fileName.Equals("")){string Allpath = System.Web.HttpContext.Current.Server.MapPath("/") + savePath;//生成原圖 System.IO.Stream stream = System.IO.File.OpenRead(Allpath + fileName);System.Drawing.Image oImage = System.Drawing.Image.FromStream(stream);stream.Close();stream.Dispose();System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);string fileType = fileName.Substring(fileName.LastIndexOf(".") + 1);int oWidth = oImage.Width;int oHeight = oImage.Height;int tWidth = destWidth; //設置縮略圖初始寬度 int tHeight = destHeight; //設置縮略圖初始高度//按指定寬高縮放if (type == 1){tWidth = destWidth;tHeight = destHeight;}//按比例計算出縮略圖的寬度和高度 else if (type == 2){if (oWidth > tWidth || oHeight > tHeight){if (oWidth >= oHeight){tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));}else{tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));}}else{tWidth = oWidth; //原圖寬度 tHeight = oHeight; //原圖高度}}//指定寬度,寬度大于指定寬度按指定寬度進行等比縮放,小于指定寬度按原圖大小上傳else if (type == 3){if (oWidth >= tWidth){if (oWidth >= oHeight){tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));}else{tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));}}else{tWidth = oWidth; //原圖寬度 tHeight = oHeight; //原圖高度}}else{tWidth = oWidth; //原圖寬度 tHeight = oHeight; //原圖高度}//生成縮略原圖 oImage = oImage.GetThumbnailImage(tWidth, tHeight, callb, IntPtr.Zero);oImage.Save(Allpath+fileName);}}public static bool ThumbnailCallback() { return false; }#endregion
轉載于:https://www.cnblogs.com/fogwang/p/3930831.html
總結
以上是生活随笔為你收集整理的asp.net上传图片文件自动修改图片大小代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ios序列化与反序列化,本地化
- 下一篇: WPF学习开发客户端软件-任务助手(下