在上传到 Blob 存储之前调整 IFormFile 的大小
Resize IFormFile before Upload to Blob Storage
我目前正在通过以下函数将图像列表上传到 azure blob 存储(路径然后存储在数据库中)。
这一切都很好,但是如果图像大于某个分辨率,我想调整大小以希望减小图像文件的大小(这显然是。
我找到了裁剪 'Image' 的示例,但是因为这是一个 IFormFile 列表(来自输入类型=上传),所以它的工作方式似乎不同。我已经尝试将 IFormFile 转换为图像,然后调整图像大小,但是我无法转换回 IFormFile。
任何帮助或指点都会很棒。谢谢
public async Task UploadImagesAsync(IFormFileCollection files, int VehicleID)
{
var connectionString = _configuration.GetConnectionString("AzureStorageAccount");
var container = _uploadService.GetBlobContainer(connectionString);
foreach (var file in files)
{
// Resize file here
// parse the content disposition header
var contentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
// grab file name
var fileName = "Vehicles/" + VehicleID + "/" + contentDisposition.FileName.Trim('"');
// get reference
var blockBlob = container.GetBlockBlobReference(fileName);
// upload
await blockBlob.UploadFromStreamAsync(file.OpenReadStream());
根据我的研究,我们可以使用 SixLabors 来调整应用程序中的图像大小。
例如
public async Task UploadImagesAsync(IFormFileCollection files, int VehicleID)
{
foreach (var file in files)
{
var extension = Path.GetExtension(file.FileName);
var encoder = GetEncoder(extension);
if (encoder != null)
{
using (var output = new MemoryStream())
using (Image<Rgba32> image = Image.Load(input))
{
var divisor = image.Width / thumbnailWidth;
var height = Convert.ToInt32(Math.Round((decimal)(image.Height / divisor)));
image.Mutate(x => x.Resize(thumbnailWidth, height));
image.Save(output, encoder);
output.Position = 0;
await blockBlob.UploadFromStreamAsync(output);
}
}
}
}
private static IImageEncoder GetEncoder(string extension)
{
IImageEncoder encoder = null;
extension = extension.Replace(".", "");
var isSupported = Regex.IsMatch(extension, "gif|png|jpe?g", RegexOptions.IgnoreCase);
if (isSupported)
{
switch (extension)
{
case "png":
encoder = new PngEncoder();
break;
case "jpg":
encoder = new JpegEncoder();
break;
case "jpeg":
encoder = new JpegEncoder();
break;
case "gif":
encoder = new GifEncoder();
break;
default:
break;
}
}
我目前正在通过以下函数将图像列表上传到 azure blob 存储(路径然后存储在数据库中)。
这一切都很好,但是如果图像大于某个分辨率,我想调整大小以希望减小图像文件的大小(这显然是。
我找到了裁剪 'Image' 的示例,但是因为这是一个 IFormFile 列表(来自输入类型=上传),所以它的工作方式似乎不同。我已经尝试将 IFormFile 转换为图像,然后调整图像大小,但是我无法转换回 IFormFile。
任何帮助或指点都会很棒。谢谢
public async Task UploadImagesAsync(IFormFileCollection files, int VehicleID)
{
var connectionString = _configuration.GetConnectionString("AzureStorageAccount");
var container = _uploadService.GetBlobContainer(connectionString);
foreach (var file in files)
{
// Resize file here
// parse the content disposition header
var contentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
// grab file name
var fileName = "Vehicles/" + VehicleID + "/" + contentDisposition.FileName.Trim('"');
// get reference
var blockBlob = container.GetBlockBlobReference(fileName);
// upload
await blockBlob.UploadFromStreamAsync(file.OpenReadStream());
根据我的研究,我们可以使用 SixLabors 来调整应用程序中的图像大小。 例如
public async Task UploadImagesAsync(IFormFileCollection files, int VehicleID)
{
foreach (var file in files)
{
var extension = Path.GetExtension(file.FileName);
var encoder = GetEncoder(extension);
if (encoder != null)
{
using (var output = new MemoryStream())
using (Image<Rgba32> image = Image.Load(input))
{
var divisor = image.Width / thumbnailWidth;
var height = Convert.ToInt32(Math.Round((decimal)(image.Height / divisor)));
image.Mutate(x => x.Resize(thumbnailWidth, height));
image.Save(output, encoder);
output.Position = 0;
await blockBlob.UploadFromStreamAsync(output);
}
}
}
}
private static IImageEncoder GetEncoder(string extension)
{
IImageEncoder encoder = null;
extension = extension.Replace(".", "");
var isSupported = Regex.IsMatch(extension, "gif|png|jpe?g", RegexOptions.IgnoreCase);
if (isSupported)
{
switch (extension)
{
case "png":
encoder = new PngEncoder();
break;
case "jpg":
encoder = new JpegEncoder();
break;
case "jpeg":
encoder = new JpegEncoder();
break;
case "gif":
encoder = new GifEncoder();
break;
default:
break;
}
}