c# - 如何将调整大小的图像保存到 ASP.NET Core 应用程序中的 Azure Blob 存储?
问题描述
我正在使用该ImageSharp
库在将图像上传到 Azure 之前重新缩放图像,应用程序在执行UploadBlob
操作时挂起且没有错误,我认为这是导致它的流。上传图像时,从图像流中收集信息,我创建一个空MemoryStream
的,使用调整图像大小ImageSharp
,用我新缩放的图像填充MemoryStream
并尝试将其上传MemoryStream
到 Azure,我认为它不喜欢它它挂在哪里。
MemoryStream 是在这种情况下使用的正确东西还是其他东西?
汽车控制器.cs
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Car car)
{
// Define the cancellation token.
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
if (ModelState.IsValid)
{
//Access the car record
_carService.InsertCar(car);
//Get the newly created ID
int id = car.Id;
//Give it a name with some virtual directories within the container
string fileName = "car/" + id + "/car-image.jpg";
string strContainerName = "uploads";
//I create a memory stream ready for the rescaled image, not sure this is right.
Stream outStream = new MemoryStream();
//Access my storage account
BlobServiceClient blobServiceClient = new BlobServiceClient(accessKey);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(strContainerName);
//Open the image read stream
var carImage = car.ImageFile.OpenReadStream();
//Rescale the image, save as jpeg.
using (Image image = Image.Load(carImage))
{
int width = 250;
int height = 0;
image.Mutate(x => x.Resize(width, height));
image.SaveAsJpeg(outStream);
}
var blobs = containerClient.UploadBlob(fileName, outStream);
return RedirectToAction(nameof(Index));
}
return View(car);
}
解决方案
它与 ImageSharp 库没有任何关系。
保存后需要重新设置outStream
位置。BlobContainerClient
正在尝试从流的末尾读取。
推荐阅读
- java - 如何将 for 循环输出作为字符串数组返回?
- powershell - 如何重命名仅出现 1 次的文件?
- c# - EF 核心异步查询挂起,但同步有效
- r - 展开给定条件的行
- plesk - Plesk:域的日期不是服务器的日期
- excel - 将excel列作为字符串读取,即使它包含数值而不使用apache-poi进行格式化
- javascript - JavaScript preventDefault 影响孩子
- python - 类型字符变化的 DataError 值太长(100)
- spring - How can I secure Spring controller to not accept wrong type of path variables passes in URL?
- ruby-on-rails - ruby aasm gem:事件名称更改,但允许的方法仍然要求旧事件名称