获取上传图像的纵横比(宽度和高度)
Get aspect ratio (width and height) of an uploaded image
我想在 API 中验证我上传的图片。我只想允许处于横向模式的照片。我还想检查纵横比。这是我检查 iFormFile 是否为图像的代码:
[HttpPost]
public JsonResult Post(IFormFile file)
{
if (file.ContentType.ToLower() != "image/jpeg" &&
file.ContentType.ToLower() != "image/jpg" &&
file.ContentType.ToLower() != "image/png")
{
// not a .jpg or .png file
return new JsonResult(new
{
success = false
});
}
// todo: check aspect ratio for landscape mode
return new JsonResult(new
{
success = true
});
}
由于 System.Drawing.Image
不再可用,我找不到将 iFormFile 转换为 Image 类型对象、检查宽度和高度以计算其纵横比的方法。如何在 ASP.NET Core API 2.0 中获取类型为 iFormFile 的图像的宽度和高度?
Since System.Drawing.Image
is not available anymore, I couldn't find a way to convert the iFormFile into an Image typed object, to check the width and height to calculate the aspect ratio of it.
这实际上是不正确的。 Microsoft 已将 System.Drawing.Common
作为 NuGet 发布,它提供了跨平台的 GDI+ 图形功能。 API 应该是任何旧 System.Drawing
代码的就地替换:
using (var image = Image.FromStream(file.OpenReadStream()))
{
// use image.Width and image.Height
}
我在 .net core 中遇到了类似的问题,我通过 客户端解决方案 解决了它。根据您的项目情况,您可以信任客户端处理并让他们做一些工作。您可以通过几行 js 代码获得纵横比,即 supported on all browsers,然后将这些 (width
& height
) 也传递给 API:
var file = e.target.files[0];
if (/\.(jpe?g|png|gif)$/i.test(file.name)) {
var reader = new FileReader();
reader.addEventListener("load", function () {
var image = new Image();
image.src = this.result as string;
image.addEventListener('load', function () {
console.log(`height: ${this.height}, width: ${this.width}`);
});
}, false);
reader.readAsDataURL(file);
}
而 API 看起来像:
//POST : api/samples?width=100&height=500
[HttpPost]
public JsonResult Post(IFormFile file, int width, int height) {}
我已经针对 多文件上传 测试了此解决方案并且工作正常。
我想在 API 中验证我上传的图片。我只想允许处于横向模式的照片。我还想检查纵横比。这是我检查 iFormFile 是否为图像的代码:
[HttpPost]
public JsonResult Post(IFormFile file)
{
if (file.ContentType.ToLower() != "image/jpeg" &&
file.ContentType.ToLower() != "image/jpg" &&
file.ContentType.ToLower() != "image/png")
{
// not a .jpg or .png file
return new JsonResult(new
{
success = false
});
}
// todo: check aspect ratio for landscape mode
return new JsonResult(new
{
success = true
});
}
由于 System.Drawing.Image
不再可用,我找不到将 iFormFile 转换为 Image 类型对象、检查宽度和高度以计算其纵横比的方法。如何在 ASP.NET Core API 2.0 中获取类型为 iFormFile 的图像的宽度和高度?
Since
System.Drawing.Image
is not available anymore, I couldn't find a way to convert the iFormFile into an Image typed object, to check the width and height to calculate the aspect ratio of it.
这实际上是不正确的。 Microsoft 已将 System.Drawing.Common
作为 NuGet 发布,它提供了跨平台的 GDI+ 图形功能。 API 应该是任何旧 System.Drawing
代码的就地替换:
using (var image = Image.FromStream(file.OpenReadStream()))
{
// use image.Width and image.Height
}
我在 .net core 中遇到了类似的问题,我通过 客户端解决方案 解决了它。根据您的项目情况,您可以信任客户端处理并让他们做一些工作。您可以通过几行 js 代码获得纵横比,即 supported on all browsers,然后将这些 (width
& height
) 也传递给 API:
var file = e.target.files[0];
if (/\.(jpe?g|png|gif)$/i.test(file.name)) {
var reader = new FileReader();
reader.addEventListener("load", function () {
var image = new Image();
image.src = this.result as string;
image.addEventListener('load', function () {
console.log(`height: ${this.height}, width: ${this.width}`);
});
}, false);
reader.readAsDataURL(file);
}
而 API 看起来像:
//POST : api/samples?width=100&height=500
[HttpPost]
public JsonResult Post(IFormFile file, int width, int height) {}
我已经针对 多文件上传 测试了此解决方案并且工作正常。