Kayomez
Kayomez

Reputation: 63

Blazor [WASM] Get image size

Expecting the answer to be using jsInterop but never hurts to ask.

Can the following be done in Blazor:

const img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.height);
}
img.src = 'http://www.google.com/intl/en_ALL/images/logo.gif';

As seen here:How to get image size (height & width) using JavaScript?

Upvotes: 1

Views: 2371

Answers (1)

Zsolt Bendes
Zsolt Bendes

Reputation: 2569

I was able to get this done with SixLabors.ImageSharp nuget on Blazor Wasm.

protected async override Task OnInitializedAsync()
{
    var data = await client.GetByteArrayAsync(ImageUrl);
    using(var img = SixLabors.ImageSharp.Image.Load(data))
    {
        Console.WriteLine($"{img.Height}");
    }
}

I'm not sure if it worth the additional assembly. Probably the JS interop is better for this.

Upvotes: 1

Related Questions