ASP.NET MVC 网站开发总结(三) ——图片截图上传
2016-04-29 21:15
741 查看
本着简洁直接,我们就直奔主题吧,这里需要使用到一个网页在线截图插件imgareaselect(请自行下载)。
前台页面:
View Code
上面代码是一个用户头像更新的代码,也是先将图片等比例放缩(长或宽放缩为280px),也前台页面上面的图片相对应,然后再进行裁剪。
第一次团队合作ASP.NET MVC 网站开发总结就到这里吧。
<我的博客主页>:http://www.cnblogs.com/forcheng/
前台页面:
//更新用户头像 [HttpPost] public ActionResult UpdatePicture(int x1, int y1, int x2, int y2, HttpPostedFileBase pictureFile) { if (pictureFile != null && pictureFile.ContentLength > 0) { if (pictureFile.ContentLength > 5242880) { return Content("<script>alert('错误提示:文件大小超出指定范围!');</script>"); } string fileName = pictureFile.FileName.Split('\\').Last(); string fileExt = fileName.Substring(fileName.LastIndexOf('.')).ToLower(); if (fileExt == ".jpg" || fileExt == ".png") { string path = Server.MapPath("~/Resources/HeadPicture"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } fileName = DateTime.Now.ToFileTime().ToString() + Guid.NewGuid().ToString("N") + fileExt; var picPath = Path.Combine(path, fileName); pictureFile.SaveAs(picPath);//从客户端保存文件到本地 //检查裁剪图片是否合法并裁剪图片 var image = new WebImage(picPath); double height = image.Height; double width = image.Width; if (width > height) { height = (int)(280 * (height / width)); width = 280; } else { width = (int)(280 * (width / height)); height = 280; } image.Resize((int)width, (int)height);//调整图片大小 var length = x2 - x1; if (x1 >= 0 && x1 <= 280 && y1 >= 0 && y1 <= 280 && length >= 50 && length <= 280 && x1 + length <= width && y1 + length <= height) { image.Crop(y1, x1, (int)(height - y1 - length), (int)(width - x1 - length)); image.Save(picPath); var logined = entity.Users.Find(Convert.ToInt32(User.Identity.Name)); logined.Picture = fileName; entity.SaveChanges(); return Content("<script>alert('操作成功提示:成功更新照片!');</script>"); } else { System.IO.File.Delete(picPath); return Content("<script>alert('错误提示:上传的图片信息不合法!');</script>"); } } else { return Content("<script>alert('错误提示:上传的文件格式不正确!');</script>"); } } else { return Content("<script>alert('错误提示:上传的文件是空文件!');</script>"); } }
View Code
上面代码是一个用户头像更新的代码,也是先将图片等比例放缩(长或宽放缩为280px),也前台页面上面的图片相对应,然后再进行裁剪。
第一次团队合作ASP.NET MVC 网站开发总结就到这里吧。
<我的博客主页>:http://www.cnblogs.com/forcheng/
相关文章推荐
- asp.net MVC 中 Session统一验证的方法
- ASPxGridView 导出和部分属性设置
- 1.简单实例:ASP.NET下Echarts通过Ajax从数据库中获取数据
- CS0016: 未能写入输出文件“c:/Windows/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/root/...拒绝访问
- 未能将网站xx 配置为使用 ASP.NET 4.0。为了使此网站正确运行,您必须将它手动配置为使用 ASP.NET 4.0。
- asp.net 配置 web.config 禁用VS2013自带的Browser Link功能
- java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
- 【Raspberry Pi 3试用体验】+ngrok 内网穿透
- ireport/jasprereport studio 导出excel最后有空白行
- 【Raspberry Pi 3试用体验】+ 使用owncloud云服务
- ASP.NET 状态管理(视图状态 ViewState)
- 了解ASP.NET MVC几种ActionResult的本质:EmptyResult & ContentResult
- 了解ASP.NET MVC几种ActionResult的本质:FileResult
- ASP.NET5使用FaceBook登录
- Aspose word导出照片代码实现
- Asp.net下使用RequiredFieldValidator影响别的控件的问题
- ASP.NET MVC中错误日志信息记录
- ASP.NET MVC异步处理请求的意义
- 在ASP.NET 2.0中操作数据之五:声明参数
- ASP.NET MVC SSO单点登录设计与实现