Search code examples
c#azureazure-blob-storagesixlabors.imagesharp

C# SixLabors.ImageSharp Image.Load<Rgba32> from Azure Blob URL


  • I have written some code that uploads images to Azure Blob Storage in the cloud.
  • I then want to load the image from a cloud URL, using SixLabors.ImageSharp, and resize the image.

How do I get the below code to read from a cloud URL, as opposed to a local file path (stored on a hard drive)?

string imageFilePathUrl = GetImageFilePathUrlFromAzureBlob();
using (Image<Rgba32> image = Image.Load<Rgba32>(imageFilePathUrl))
{
    //Resize the loaded image URL...
}

Solution

  • I don't see an Image.Load overload that takes a URL string (https://docs.sixlabors.com/api/ImageSharp/SixLabors.ImageSharp.Image.html#SixLabors_ImageSharp_Image_Load_System_String_). It will interpret the string as a file path, which it isn't.

    You must download the image first, as it's not stored locally. You can use some Azure's storage client, or just download it with a HttpRequest. The easiest way would be with Flurl (https://flurl.dev/):

    using Flurl;
    using Flurl.Http;
    
    string imageFilePathUrl = GetImageFilePathUrlFromAzureBlob();
    Stream stream = await imageFilePathUrl.GetStreamAsync();
    using (Image<Rgba32> image = Image.Load<Rgba32>(stream))
    {
        //Resize the loaded image URL...
    }