c# - 调整 HttpFile 类型的图像大小
问题描述
我正在尝试使用 C# 中的 API 将图像上传到服务器,但在上传之前,我希望调整图像的大小。下面是我用来上传图像而不调整大小的代码。
private dynamic UploadImage(dynamic parameters)
{
var files = Request.Files;
var isProfilePic = Request.Form["IsProfileImage"].Value;
string virImgpath = string.Empty;
if (files.Count() > 0)
{
HttpFile file = files.FirstOrDefault();
string _imgname = string.Empty;
if (file.Value != null)
{
using (MemoryStream ms = new MemoryStream())
{
//code to upload
}
_userRepository.SaveImage(CurrentUser.Id, (isProfilePic == "true"), _imgname); // virImgpath
if (isProfilePic == "true")
{
CurrentUser.ProfileImage = virImgpath;
}
else
{
CurrentUser.BackgroundImage = virImgpath;
}
}
}
return Response.AsJson(virImgpath);
}
现在,我正在尝试通过添加以下代码来实现调整大小的功能。
WebImage img = files.FirstOrDefault();
img.Resize(1000, 1000);
files = (IEnumerable<HttpFile>)img;
问题是文件属于该HttpFile
类型,并且该Resize
方法需要该WebImage
类型的对象。
解决方案
WebImage
您可以通过使用构造函数传入流来创建一个新的
var webImage = new WebImage(file.InputStream);
webImage.Resize(...,...,...,...);
https://msdn.microsoft.com/en-us/library/gg537958(v=vs.111).aspx
推荐阅读
- python - Python中是否有特定于属性的方法/接口?
- php - Laravel 工匠版
- typescript - 组合来自多个 RxJS 可观察对象的数据的更好方法
- amazon-web-services - Backblaze S3 兼容 API - 预签名 URL - 节点 AWS 开发工具包不起作用
- python - 如何在不出现 415 错误的情况下向 API 发送 PUT 请求
- rust - 如果 main 返回 Err,Rust 程序会返回什么退出代码
- loops - rust 借用检查看起来很聪明,它可以检查和扁平化循环的读写。但我怎样才能绕过它?
- excel - Characters 对象的更快替代方案
- angular - 将 OnPush 设置为 nrwl-nx 中所有应用程序的默认值
- javascript - 顶点着色器中未声明具有相同名称但类型不同的变量,或片段着色器中静态使用的变量:fogDepth