2

I am making a page in asp.net core that is uploading images to server using input type file html control.

<form method="post" asp-action="Add" enctype="multipart/form-data">
    <input type="file" name="mediaUpload" />
    <button type="submit">Submit</button>
</form>

I have also added the System.Drawing for .NET Core dll from NuGet.

Now on my post action I want to get the image Width & Height. This is the below code for it (but its now working).

[HttpPost]
public ActionResult Add(IFormFile mediaUpload)
{
    Image image = Image.FromStream(file.OpenReadStream); // error is here
    int width = image.Width;
    int height = image.Height;
}  

The complile time error -

Severity Code Description Project File Line Suppression State Error CS1503 Argument 1: cannot convert from 'method group' to 'Stream' Goldentaurus E:\Website\Goldentaurus\Goldentaurus\Controllers\MediaController.cs 453 Active

Please help??

Note: My previous ASP.NET MVC 5 application(code shown below) was using a similar code that was working very well. I modified it to the above code to work on asp.net core.

[HttpPost]
public ActionResult Add(HttpPostedFileBase mediaUpload)
{
    Image image = Image.FromStream(file.InputStream);
    int width = image.Width;
    int height = image.Height;
}
1
  • 4
    OpenReadStream is a function, so you need to add () to invoke it. Commented May 16, 2018 at 18:33

1 Answer 1

1

You can try this link https://andrewlock.net/using-imagesharp-to-resize-images-in-asp-net-core-a-comparison-with-corecompat-system-drawing/ it explains how to use CoreCompat.System.Drawing which is what you are supposed to use for .net core. It also compares it with ImageSharp. I used ImageSharp because I dont have to specify the operating system as is required in CoreCompat.System.Drawing, here is how I got it to work using the nuget package SixLabors.ImageSharp 1.0.0-beta0005 (make sure the Include prerelease is box in nugget is checked)

 private async Task<(int height, int width)> GetImageDimentions(IFormFile file)
    {
        if (file != null)
        {
            List<string> AcceptableImageExtentions = new List<string> { ".jpg", ".jpeg", ".png", ".bmp" };

            string fileExtention = System.IO.Path.GetExtension(file.FileName);

            if (AcceptableImageExtentions.Contains(fileExtention))
            {
                using (System.IO.Stream stream = new System.IO.MemoryStream())
                {
                    await file.CopyToAsync(stream);
                    SixLabors.ImageSharp.Formats.IImageDecoder imageDecoder;

                    if (fileExtention == ".jpeg" || fileExtention == ".jpg")
                    {
                        imageDecoder = new SixLabors.ImageSharp.Formats.Jpeg.JpegDecoder();
                    }
                    else if (fileExtention == ".png")
                    {
                        imageDecoder = new SixLabors.ImageSharp.Formats.Png.PngDecoder();
                    }
                    else
                    {
                        imageDecoder = new SixLabors.ImageSharp.Formats.Bmp.BmpDecoder();
                    }


                    if (stream.Position == stream.Length) //Check this because if your image is a .png, it might just throw an error
                    {
                        stream.Position = stream.Seek(0, SeekOrigin.Begin);
                    }

                    SixLabors.ImageSharp.Image<SixLabors.ImageSharp.PixelFormats.Rgba32> imageSharp = imageDecoder.Decode<SixLabors.ImageSharp.PixelFormats.Rgba32>(Configuration.Default, stream);

                    if (imageSharp != null)
                    {
                        return (imageSharp.Height, imageSharp.Width);
                    }
                }
            }
        }
        return (0, 0);
    }

You can also read https://blogs.msdn.microsoft.com/dotnet/2017/01/19/net-core-image-processing/ Hope it helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.