.net 上传图片压缩裁剪,压缩后大小一般不会变大
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | [HttpPost] [AuthorizeIgnore] public ActionResult PhoneUpload() { try { var files = Request.Files; if (files == null || files.Count == 0) throw new Exception( "请选择需要上传的图片" ); var upFile = files[0]; var allowType = new string [] { "image/jpeg" , "image/jpg" , "image/png" , "image/gif" }; if (!allowType.Contains(upFile.ContentType)) throw new Exception( "请上传“.jpg/.png/.gif”格式的图片" ); if (upFile.ContentLength > 1024 * 1024 * 20) throw new Exception( "图片大小不能超20M" ); string ext = upFile.FileName.Substring(upFile.FileName.LastIndexOf( '.' )); var guid = Guid.NewGuid(); string strpath = Path.Combine( "UploadFiles" , "imagebanklog" , guid + ext); string strpath_th = Path.Combine( "UploadFiles" , "imagebanklog" , guid + "_th" + ext); string path = Path.Combine(Server.MapPath( "~/" ), strpath); string path_th = Path.Combine(Server.MapPath( "~/" ), strpath_th); upFile.SaveAs(path); GetThumImage(path, 90, 1024, path_th); try { System.IO.File.Delete(path); } catch { } return Json( new { id = 0, success = true , result = strpath_th }, "text/html" ); } catch (Exception ex) { return Json( new { id = 0, success = false , msg = ex.Message.ToString() }, "text/html" ); } } /// <summary> /// 生成缩略图 /// </summary> /// <param name="sourceFile">原始图片文件</param> /// <param name="quality">质量压缩比,0-100,100是最清晰</param> /// <param name="width">最大宽度</param> /// <param name="outputFile">输出文件名</param> /// <returns>成功返回true,失败则返回false</returns> private bool GetThumImage( string sourceFile, long quality, int width, string outputFile) { try { long imageQuality = quality; Bitmap sourceImage = new Bitmap(sourceFile); var oWidth = sourceImage.Width; var oHeight = sourceImage.Height; var height = oHeight; if (width > oWidth) { width = oWidth; } else { height = width * oHeight / oWidth; } var myImageCodecInfo = GetEncoderInfo( "image/jpeg" ); var myEncoder = System.Drawing.Imaging.Encoder.Quality; var myEncoderParameters = new EncoderParameters(1); var myEncoderParameter = new EncoderParameter(myEncoder, imageQuality); myEncoderParameters.Param[0] = myEncoderParameter; float xWidth = sourceImage.Width; float yWidth = sourceImage.Height; Bitmap newImage = new Bitmap(width, height); Graphics g = Graphics.FromImage(newImage); g.DrawImage(sourceImage, 0, 0, width, height); g.Dispose(); newImage.Save(outputFile, myImageCodecInfo, myEncoderParameters); sourceImage.Dispose(); sourceImage = null ; newImage.Dispose(); newImage = null ; return true ; } catch { return false ; } } /// <summary> /// 获取图片编码信息 /// </summary> private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; encoders = ImageCodecInfo.GetImageEncoders(); for (j = 0; j < encoders.Length; ++j) { if (encoders[j].MimeType == mimeType) return encoders[j]; } return null ; } |
分类:
C#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构