来自Azure的.net核心流文件并调整大小

时间:2019-02-18 14:36:19

标签: c# asp.net-core imagesharp

我试图从Azure blob中获取文件,使用ImageSharp调整其大小,然后将其提供给客户端。我有以下代码可以正常工作:

    public async Task<IActionResult> GetFile(string url, int width =0, int height=0) {
        // Connect to Azure
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference("images");
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(url);


        var azureStream = new MemoryStream();          
        await blockBlob.DownloadToStreamAsync(azureStream);
        azureStream.Seek(0, SeekOrigin.Begin);

        var image = Image.Load(azureStream);
        image.Mutate(x=>x.Resize(new ResizeOptions(){
            Size = new Size(width, height),
            Mode = ResizeMode.Pad

        }));

        var stream = new MemoryStream();
        image.SaveAsPng(stream);
        stream.Seek(0, SeekOrigin.Begin);
        return File(stream, "image/png");

    }

与创建2个内存流相比,有没有更有效的方法?最终将需要大量的请求,我真的希望它表现得尽可能好。

0 个答案:

没有答案