[英]How to compress an image from IFormFile before upload to the server ASP.Net Core
I have a problem, I'm creating an app in net core, to upload kids information, but, the main problem is than all the image I have are from my phone and you know than we are talking about 9-15 MB per picture, so, I know than I can tell the user "There's a limitation" but, I thinks than that its not useful, so, There's a way to reduce the size of the image loosing the less quality possible?.我有一个问题,我正在 net core 中创建一个应用程序来上传孩子的信息,但是,主要问题是我拥有的所有图像都来自我的手机,你知道我们谈论的是每张图片 9-15 MB ,所以,我知道我不能告诉用户“有一个限制”,但是,我认为它没有用,所以,有一种方法可以减少图像的大小,从而降低质量吗?。
This is my Method这是我的方法
Class班级
public IFormFile ImageFile { get; set; }
Method方法
if (vm.ImageFile != null && vm.ImageFile.Length > 0)
{
var guid = Guid.NewGuid().ToString();
var file = $"{guid}.jpg";
path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images\\Kids", file);
using (var stream = new FileStream(path, FileMode.Create))
{
//var convertedImage = MagicSolutionGivenByTheAwsomeStackOverFlowCommunity
await vm.ImageFile.CopyToAsync(stream);
}
}
Layout布局
<form asp-action="Create" enctype="multipart/form-data">
<input type="hidden" asp-for="Imagen"/>
<div class="col-sm-4">
<label asp-for="Imagen" class="control-label"></label>
<div>
<input asp-for="ImageFile" class="form-control filestyle"
type="file" data-classbutton="btn btn-secondary"
data-classinput="form-control inline"
data-icon="<span class='fa fa-upload mr'></span>" />
</div>
<span asp-validation-for="Imagen" class="text-danger"></span>
</div>
</form>
For my projects I usually use this library on backend: ImageResizer Here from Nuget: nuget ImageResizer对于我的项目,我通常在后端使用这个库: ImageResizer Here from Nuget: nuget ImageResizer
It's useful for compression and resizing server-side它对于压缩和调整服务器端的大小很有用
Instead I use this in JS on frontend: PlUpload相反,我在前端的 JS 中使用它: PlUpload
In this example you can resize the image on the client Image-Resizing-on-Client-Side or you can use Chunking to speed up the upload to the client and manage the file on the server.在此示例中,您可以在客户端Image-Resizing-on-Client-Side 上调整图像大小,或者您可以使用分块来加速上传到客户端并管理服务器上的文件。
You can use ImageSharp to resize your image.您可以使用ImageSharp调整图像大小。 We used it to make a Thumb image of an uploaded IFormFile.
我们用它来制作上传的 IFormFile 的 Thumb 图像。 First you open a read stream and then open an ImageSharp image for that stream.
首先打开一个读取流,然后为该流打开一个 ImageSharp 图像。
using (var stream = file.OpenReadStream())
using (var image = Image.Load(stream))
{
await ResizeAndUploadImageAsync(image, maxWidth, fileName);
}
After that you use a MemoryStream to save the resized image as a PNG and in our case upload the file to a S3 bucket.之后,您使用 MemoryStream 将调整大小的图像保存为 PNG,并在我们的示例中将文件上传到 S3 存储桶。
private async Task ResizeAndUploadImageAsync(Image<Rgba32> image, int maxWidth, string
fileName)
{
using (var writeStream = new MemoryStream())
{
if (image.Width > maxWidth)
{
var thumbScaleFactor = maxWidth / image.Width;
image.Mutate(i => i.Resize(maxWidth, image.Height *
thumbScaleFactor));
}
image.SaveAsPng(writeStream);
await storageService.UploadFileAsync(writeStream, fileName);
}
}
我认为您关注的是一个 JS 库,它可以像Compressor JS一样压缩图像,也许这个库可以帮助您解决问题
You cannot meaningfully compress images, especially not JPEG images.您无法有意义地压缩图像,尤其是 JPEG 图像。 At least not in a reversible way.
至少不是可逆的。 If the pictures coming from your phone's camera are 9-15 MB, then you need to lower the resolution, the quality or both.
如果来自手机相机的图片为 9-15 MB,则您需要降低分辨率、质量或两者兼而有之。
You can do so in your client, using JavaScript, where you can resize the image and decrease the quality, and upload that modified image.您可以在客户端中使用 JavaScript 执行此操作,您可以在其中调整图像大小并降低质量,然后上传修改后的图像。 This has the benefit that it also saves bandwidth (the smaller image is uploaded), but the drawback is that it can be circumvented by the user if they want to.
这样做的好处是它还可以节省带宽(上传较小的图像),但缺点是用户可以根据需要绕过它。
Or you can do the modifications serverside, where there exist plenty of managed libraries to do so.或者您可以在服务器端进行修改,那里有很多托管库可以这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.