我正在尝试从 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 个内存流相比,有没有更有效的方法来做到这一点?这最终将需要大量的请求,我真的希望它尽可能地高效。