c# - 获取上传图片的纵横比(宽高)
问题描述
我想在我的 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 类型的图像的宽度和高度?
解决方案
由于
System.Drawing.Image
不再可用,我找不到将 iFormFile 转换为 Image 类型对象的方法,以检查宽度和高度以计算它的纵横比。
这实际上是不正确的。Microsoft 已System.Drawing.Common
作为NuGet发布,它提供跨平台 GDI+ 图形功能。System.Drawing
API 应该是任何旧代码的就地替换:
using (var image = Image.FromStream(file.OpenReadStream()))
{
// use image.Width and image.Height
}
推荐阅读
- c++ - 为什么我的程序没有做适当的对角线差(C++)?
- php - 如何使用 PayPal 智能按钮在商品缺货时显示已售罄消息
- c++ - 如何解决 C++ 测试单元中的链接器错误
- math - HLSL 中的高效条件天花板和地板
- python - 将环境变量传递给 docker-compose
- python-pptx - 如何检测 pptx 幻灯片中所有现有的 PNG/JPG 图像,然后使用 python pptx 操作它们的大小/布局?
- javascript - Axios 不会立即更新状态
- azure-active-directory - Azure AD 企业应用程序 - 识别调用者应用程序
- r - 从源代码编译 R 包的多个不同错误
- c# - 通过 ajax 将 XML 属性与 Elements 一起发送到 C# 控制器方法并解析以保存