通常用户上传的图片需要压缩或者生成缩略图。用System.Web.Helpers.WebImage的Resize方法可以很方便的实现这一功能。示例代码如下:
1 /// <summary>
2 /// 保存缩略图
3 /// </summary>
4 /// <param name="file">用户上传的文件</param>
5 /// <param name="fileName">想要保存的文件名</param>
6 /// <param name="uploadPath">上传地址</param>
7 /// <returns></returns>
8 private bool SaveThumbFile(HttpPostedFileBase file, string fileName, string uploadPath, int postId)
9 {
10 try
11 {
12 var thumbImage = new WebImage(file.InputStream);
13 thumbImage.Resize(80, 80);
14 var thumbPath = Path.Combine(Server.MapPath("~/Content/uploads/Images/Thumb"),
15 fileName);
16 thumbImage.Save(thumbPath);
17 return true;
18 }
19 catch
20 {
21 return false;
22 }
23 }
MVC.Net:压缩/保存图片缩略图,布布扣,bubuko.com
时间: 2024-09-30 19:16:40