Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the dimensions of the uploaded image? [duplicate]

How to get the dimensions of an uploaded image in ASP.NET MVC?

This is my FileHelper class to upload a file:

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

But still I do not know if it is possible in to check the dimensions of the uploaded image in this class.

like image 315
Mohamad Haidar Avatar asked Dec 14 '22 12:12

Mohamad Haidar


1 Answers

I think the link here could be useful

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

you can get the InputStream, it is available as property in the HttpPostedFileBase object, easily you can get it if you write file.InputStream

like image 54
Mohamad Haidar Avatar answered Jan 17 '23 21:01

Mohamad Haidar