如何将调整大小后的图像保存到 ASP.NET 核心应用程序中的 Azure Blob 存储?
How do I save my resized image to my Azure Blob Storage in my ASP.NET Core Application?
我正在使用 ImageSharp
库在将图像上传到 Azure 之前重新缩放图像,应用程序在到达 UploadBlob
操作时挂起且没有错误,我认为这是流导致的它。上传图像时,从图像流中收集信息,我创建一个空 MemoryStream
,使用 ImageSharp
调整图像大小,用我新缩放的图像填充 MemoryStream
并尝试上传MemoryStream
到 Azure,我认为它不喜欢它,因为它挂在那里。
在此实例中使用 MemoryStream 是正确的还是其他?
CarController.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
正在尝试从流的末尾读取。
我正在使用 ImageSharp
库在将图像上传到 Azure 之前重新缩放图像,应用程序在到达 UploadBlob
操作时挂起且没有错误,我认为这是流导致的它。上传图像时,从图像流中收集信息,我创建一个空 MemoryStream
,使用 ImageSharp
调整图像大小,用我新缩放的图像填充 MemoryStream
并尝试上传MemoryStream
到 Azure,我认为它不喜欢它,因为它挂在那里。
在此实例中使用 MemoryStream 是正确的还是其他?
CarController.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
正在尝试从流的末尾读取。