如何将调整大小后的图像保存到ASP.NET Core应用程序中的Azure Blob存储中?

问题描述 投票:0回答:1

我正在使用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);
}
c# asp.net-core azure-storage-blobs azure-blob-storage imagesharp
1个回答
0
投票

它与ImageSharp库没有任何关系。

保存后,您需要重置outStream位置。 BlobContainerClient正在尝试从流的末尾读取。

© www.soinside.com 2019 - 2024. All rights reserved.