简体   繁体   English

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

[英]How do I save my resized image to my Azure Blob Storage in my ASP.NET Core Application?

I'm using the ImageSharp library to rescale my images before they are uploaded to Azure, the application hangs with no errors when it reaches the UploadBlob action and I think it's the stream causing it.在将图像上传到 Azure 之前,我正在使用ImageSharp库重新缩放我的图像,应用程序在到达UploadBlob操作时挂起且没有错误,我认为是 stream 导致它。 When the image is uploaded the information is gathered from the image stream, I create an empty MemoryStream , resize the image using ImageSharp , stuff the MemoryStream with my newly scaled image and try to upload that MemoryStream to Azure and I don't think it likes it as that is where it hangs.上传图像时,从图像 stream 收集信息,我创建一个空MemoryStream ,使用ImageSharp调整图像大小,用我新缩放的图像填充MemoryStream并尝试将该MemoryStream上传到 Azure 我不认为它喜欢它就挂在那里。

Is MemoryStream the correct thing to use in this instance or is it something else? MemoryStream 是在这种情况下使用的正确东西还是其他东西?

CarController.cs汽车控制器.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);
}

It's not got anything to do with the ImageSharp library.它与 ImageSharp 库没有任何关系。

You need to reset your outStream position after saving.保存后,您需要重置outStream position。 BlobContainerClient is trying to read from the end of the stream. BlobContainerClient正在尝试从 stream 的末尾读取。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 ASP.NET Core 应用程序中显示来自 Azure Blob 存储帐户的图像? - How to I display an image from my Azure Blob Storage Account in my ASP.NET Core Application? 如何从 ASP.NET 核心应用程序将图像上传到 Azure blob 存储中的特定目录? - How do I upload an image to a specific directory in my Azure blob storage from my ASP.NET Core application? 为什么我不能从我的 ASP.NET 核心应用程序中删除我的 Azure 存储帐户中的任何 blob? - Why cannot I not delete any blobs in my Azure Storage account from my ASP.NET Core Application? 如何在我的 ASP.NET Core 应用程序中显示和更新我的自定义标识字段? - How do I display and update my custom Identity fields in my ASP.NET Core application? 如何从我的 ASP.NET 核心应用程序访问来自 Azure Active Directory 的其他数据? - How do I access additional data from Azure Active Directory from my ASP.NET Core application? 如何从 ASP.NET Core 中的私有 Azure Blob Stroage 帐户将图像返回到视图? - How do I return an image to a view from a private Azure Blob Stroage account in ASP.NET Core? 如何在我的 ASP.NET 核心应用程序中返回容器中的 blob 列表? - How do I return a list of blobs in a container within my ASP.NET Core application? 启动ASP.NET Core应用程序后如何启动Web浏览器? - How do I launch the web browser after starting my ASP.NET Core application? 如何在我的 ASP.NET 核心应用程序中捕获 KendoUI 多选数据? - How do I capture KendoUI Multiselect data in my ASP.NET Core Application? 如何允许从我的 ASP.NET Core 应用程序覆盖 blob? - How do I allow the overwriting of blobs from my ASP.NET Core application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM