I am trying to take a file from an Azure blob, resize it using ImageSharp, and serve it to the client. I have the following code, which works:
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");
}
Is there a more efficient way to do this vs creating 2 memory streams? This will eventually take tons of requests and I really want it to be as performant as possible.