Asp.net core 如何获得图像宽度&;在asp.net core中进行上载时的高度
我正在asp.net core中创建一个页面,即使用输入类型文件html控件将图像上传到服务器Asp.net core 如何获得图像宽度&;在asp.net core中进行上载时的高度,asp.net-core,asp.net-core-mvc,Asp.net Core,Asp.net Core Mvc,我正在asp.net core中创建一个页面,即使用输入类型文件html控件将图像上传到服务器 <form method="post" asp-action="Add" enctype="multipart/form-data"> <input type="file" name="mediaUpload" /> <button type="submit">Submit</button> </form> complile
<form method="post" asp-action="Add" enctype="multipart/form-data">
<input type="file" name="mediaUpload" />
<button type="submit">Submit</button>
</form>
complile时间错误-
严重性代码说明项目文件行抑制状态
错误CS1503参数1:无法从“方法组”转换为
“Stream”Goldentaurus E:\Website\Goldentaurus\Goldentaurus\Controllers\MediaController.cs 453处于活动状态
请帮忙
注意:我以前的ASP.NET MVC 5应用程序(代码如下所示)使用的是一个运行良好的类似代码。我将其修改为上面的代码,以便在asp.net core上工作。
[HttpPost]
public ActionResult Add(HttpPostedFileBase mediaUpload)
{
Image image = Image.FromStream(file.InputStream);
int width = image.Width;
int height = image.Height;
}
您可以尝试此链接,它解释了如何使用CoreCompat.System.Drawing,这是您应该用于.net core的。它还将其与ImageSharp进行了比较。我使用ImageSharp是因为我不必按照CoreCompat.system.Drawing中的要求指定操作系统,下面是我如何使用nuget软件包SixLabors.ImageSharp 1.0.0-Beta005使其工作的(确保选中了nugget中的Include prerelease is框)
专用异步任务GetImageDimensions(IFormFile)
{
如果(文件!=null)
{
List AcceptableImageExtensions=新列表{.jpg“,.jpeg“,.png“,.bmp”};
字符串fileextension=System.IO.Path.GetExtension(file.FileName);
if(AcceptableImageExtensions.Contains(FileExtension))
{
使用(System.IO.Stream=new System.IO.MemoryStream())
{
等待文件.CopyToAsync(流);
SixLabors.ImageSharp.Formats.IImageDecoder图像解码器;
如果(文件扩展==“.jpeg”| |文件扩展==“.jpg”)
{
imageDecoder=新的SixLabors.ImageSharp.Formats.Jpeg.JpegDecoder();
}
else if(fileExtention==“.png”)
{
imageDecoder=new SixLabors.ImageSharp.Formats.Png.PngDecoder();
}
其他的
{
imageDecoder=new SixLabors.ImageSharp.Formats.Bmp.BmpDecoder();
}
if(stream.Position==stream.Length)//检查此项,因为如果图像是.png,则可能会抛出错误
{
stream.Position=stream.Seek(0,SeekOrigin.Begin);
}
SixLabors.ImageSharp.Image ImageSharp=imageDecoder.Decode(Configuration.Default,stream);
如果(imageSharp!=null)
{
返回值(imageSharp.Height,imageSharp.Width);
}
}
}
}
返回(0,0);
}
你也可以阅读
希望对您有所帮助
OpenReadStream
是一个函数,所以您需要添加()
来调用它。
[HttpPost]
public ActionResult Add(HttpPostedFileBase mediaUpload)
{
Image image = Image.FromStream(file.InputStream);
int width = image.Width;
int height = image.Height;
}
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);
}