首页 > 解决方案 > 在asp.net核心中进行上传时如何获取图像宽度和高度

问题描述

我正在使用输入类型文件html 控件将图像上传到服务器的 asp.net 核心中创建一个页面。

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

我还从NuGet添加了System.Drawing for .NET Core dll 。

现在在我的帖子操作中,我想获取图像的宽度和高度。这是它的以下代码(但它现在可以工作)。

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

编译时错误 -

严重性代码描述项目文件行抑制状态错误 CS1503 参数 1:无法从“方法组”转换为“流”Goldentaurus E:\Website\Goldentaurus\Goldentaurus\Controllers\MediaController.cs 453 活动

请帮忙??

注意:我之前的 ASP.NET MVC 5 应用程序(代码如下所示)使用了一个运行良好的类似代码。我将其修改为上面的代码以在 asp.net 核心上工作。

[HttpPost]
public ActionResult Add(HttpPostedFileBase mediaUpload)
{
    Image image = Image.FromStream(file.InputStream);
    int width = image.Width;
    int height = image.Height;
}

标签: asp.net-coreasp.net-core-mvc

解决方案


您可以尝试此链接https://andrewlock.net/using-imagesharp-to-resize-images-in-asp-net-core-a-comparison-with-corecompat-system-drawing/它解释了如何使用 CoreCompat。 System.Drawing 这是您应该用于 .net 核心的内容。它还将它与 ImageSharp 进行比较。我使用 ImageSharp 是因为我不必按照 CoreCompat.System.Drawing 中的要求指定操作系统,这是我使用 nuget 包 SixLabors.ImageSharp 1.0.0-beta0005 使其工作的方法(确保包含预发布框在掘金被检查)

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

您还可以阅读https://blogs.msdn.microsoft.com/dotnet/2017/01/19/net-core-image-processing/ 希望对您有所帮助


推荐阅读