MVC中的图片验证

Wha*_*sUP 8 c# asp.net asp.net-mvc asp.net-mvc-2

目标:
评估图片的格式,宽度和高度,然后将其保存在我的程序中.

问题:
不知道如何使用HttpPostedFileBase file然后发送它Image newImage = Image.FromFile(xxxx);而不保存我的程序中的图片.

  1. 验证
  2. 在我的"App_Data"中保存图片
[AcceptVerbs(HttpVerbs.Post)]  
public ActionResult Add(HttpPostedFileBase file)  
{
    if (file.ContentLength > 0)
    {
        Image newImage = Image.FromFile(xxxx);      
    }

    return Index();  
 } 
Run Code Online (Sandbox Code Playgroud)

Mar*_*erl 12

你可以像下面的代码片段那样做.注意System.Drawing命名空间引用,您将需要该Image.FromStream()方法.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Add(HttpPostedFileBase httpPostedFileBase)
{
    using (System.Drawing.Image image = System.Drawing.Image.FromStream(httpPostedFileBase.InputStream, true, true))
    {
        if (image.Width == 100 && image.Height == 100)
        {
            var file = @"D:\test.jpg";
            image.Save(file);
        }
    }

    return View();
}
Run Code Online (Sandbox Code Playgroud)