Controller:
MVC 3.0 上传图片并生成缩略图
转载 public ActionResult Upload()
{
return View();
}
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
var ostream = file.InputStream;
var orp_w_picpath = Image.FromStream(ostream);
int owidth = orp_w_picpath.Width; //原图宽度
int oheight = orp_w_picpath.Height; //原图高度
int objwidth = 100; //设置缩略图初始宽度
int objheight = 100; //设置缩略图初始高度
//按比例计算出缩略图的宽度和高度
if (owidth >= oheight)
{
objheight = (int)Math.Floor(Convert.ToDouble(oheight) * (Convert.ToDouble(objwidth) / Convert.ToDouble(owidth)));
}
else
{
objwidth = (int)Math.Floor(Convert.ToDouble(owidth) * (Convert.ToDouble(objheight) / Convert.ToDouble(oheight)));
}
Bitmap objp_w_picpath = new Bitmap(objwidth, objheight);
Graphics graphics = Graphics.FromImage(objp_w_picpath);
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
graphics.Clear(Color.Transparent); //清空画布并以透明背景色填充
graphics.DrawImage(orp_w_picpath, new Rectangle(0, 0, objwidth, objheight), new Rectangle(0, 0, owidth, oheight), GraphicsUnit.Pixel);
//rewrite p_w_picpathname
var extensionName = Path.GetExtension(file.FileName);
var oriname = "ori" + DateTime.Now.ToString("yyyyMMddHHmmss") + extensionName;
var objname = "obj" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";
var orifilePath = Path.Combine(HttpContext.Server.MapPath("/content/videos"), Path.GetFileName(oriname));
var objfilePath = Path.Combine(HttpContext.Server.MapPath("/content/videos"), Path.GetFileName(objname));
try
{
file.SaveAs(orifilePath);
objp_w_picpath.Save(objfilePath, System.Drawing.Imaging.ImageFormat.Png);
}
catch (Exception ex)
{
throw ex;
}
finally
{
//释放资源
orp_w_picpath.Dispose();
graphics.Dispose();
objp_w_picpath.Dispose();
}
return RedirectToAction("Index");
}
View:
@using (Html.BeginForm("Upload", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<label>
Filename:</label>
<input type="file" name="file" />
<input type="submit" value="Submit" />
}
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Python Django 图片上传及显示代码示例Python
-
C# MVC Ajax上传多个图片,可预览,可重复上传等
注意要加载上传组件js .关于后台的上传代码就不用说了吧?网上一大把
上传 css ajax 上传代码 tcsh -
Spring MVC 实现文件上传及批量上传
Spring MVC 通过commons-fileupload和commons.io来实现文件上传及批量上传。
Spring MVC fileupload -
MVC应用程序显示上传的图片
前两篇《MVC应用程序实现上传文件》http://www.cnblogs.com/insus/p/3590907.html和《MVC应用程序实现上传文件(续)》http://www.cnblogs.com/insus/p/3594834.html,我们练习了上传文件,当然上传图片也是一样。此篇我们练习,怎样在MVC应用程序中显示用户上传的图片。为了接近更真实的练习,Insus.NET决定对以前的程序修改一下,就是上传的目录把原来的Temp目录改为UploadFiles目录。也就是说,Temp目录保留,在应用程序中,添加多一个UploadFiles目录。再对原来的model修改一下,添加两个属性
Image Ajax Thumbnail Bitmap Graphics -
access 上传显示图片
在“SharePoint 2013技巧分享系列 - Active Directory同步显示用户照片”文中介绍了如何同步Active Directory显示用户照片,但是同步完成后,用户照片尺寸和清晰度都不是非常理想。本文将介绍如何同步Exchange Server显示高清用户照片。原理与SharePoint Server 2010相似, 在SharePoint Server 2013中存在一个用
access 上传显示图片 sharepoint 2013 exchange powershell Server
















