获取上传图片的尺寸?

时间:2015-08-08 20:41:02

标签: c# asp.net asp.net-mvc asp.net-mvc-4

如何在ASP.NET MVC中获取上传图像的尺寸?

这是我上传文件的FileHelper课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Mvc;

namespace workflow.helpers
{
    public class FileHelper
    {
        private HttpPostedFileBase file {get; set;}
        public bool ValidFile { get; set;}

        public FileHelper(HttpPostedFileBase File, string Extensions, int MaxSize=1, bool IsImage = true, string Dimensions="")
        {
            this.file = File;
            validateFile(Extensions, MaxSize, IsImage, Dimensions);
        }

        public string uploadFile()
        {
            if (this.file.ContentLength > 0)
            {
                if (this.ValidFile)
                {
                    var fileName = Path.GetFileName(this.file.FileName);
                    var path = Path.Combine(HttpContext.Current.Server.MapPath("~/sharedfiles/uploads/images"), fileName);
                    this.file.SaveAs(path);
                    return this.file.FileName;
                }
                else return "invalid file";
            }

            return "";
        }

        private void validateFile(string extensions, int MaxSize, bool IsImage = true, string Dimensions )
        {
                if (extensions.Contains(Path.GetExtension(this.file.FileName).Replace(".", "")))
                {
                    if ((((double)(file.ContentLength) / 1024) / 1024) < MaxSize)
                        this.ValidFile = true;
                }
                else
                    this.ValidFile = false;
        }
    }
}

但我仍然不知道是否可以检查此课程中上传图片的尺寸。

1 个答案:

答案 0 :(得分:11)

我认为link here可能有用

row-eq-height

您可以private string GetImageDimension() { System.IO.Stream stream = fu1.PostedFile.InputStream; System.Drawing.Image image = System.Drawing.Image.FromStream(stream); int height = image.Height; int width = image.Width; return "Height: " + height + "; Width: " + width; } 获取InputStreamproperty对象中的HttpPostedFileBase,如果您写file.InputStream

,很容易就能获得person