【问题标题】:Getting the dimensions of the uploaded image? [duplicate]获取上传图片的尺寸? [复制]
【发布时间】:2015-08-08 20:41:02
【问题描述】:

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;
        }
    }
}

但是我仍然不知道是否可以在此类中检查上传图像的尺寸。

【问题讨论】:

  • 如果链接的副本不够,请考虑搜索您帖子的标题,以澄清为什么您的问题尚未在 SO 上得到回答:bing.com/…

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


【解决方案1】:

我认为link here 可能有用

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;
}

你可以得到InputStream,它在HttpPostedFileBase对象中作为property可用,如果你写file.InputStream就很容易得到它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2012-11-20
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多