2
votes

I'm using blazor webassembly and I need to display an image that stored as byte array in the client side.

I tried

in C#:

imagesrc= Convert.ToBase64String(imageBytes);
imageDataURL = string.Format("data:image/jpeg;base64,{0}", imagesrc);

in markup:

<img src="@imageDataURL" />

but no image displayed just icon that no image.

is there any other way?

2
It should have kind-of worked. Any errors in the F12 console? - Henk Holterman
@HenkHolterman no, no errors in console - sameeh shadid
And when you use Inspect (F12, Elements), how does the <img> look? - Henk Holterman
@HenkHolterman is it matter if I'm displaying the image in the client side? the image stored as bytes no paths - sameeh shadid
Well, you can't show much on a server... - Henk Holterman

2 Answers

4
votes

I was curious which image format you specified instead of jpeg and like you said under @Henk answer it is svg as it starts with PHN2Z...

That doesn't mean you have to use some svg to jpeg converter, just adjust data uri type appropriately:

imageDataURL = string.Format("data:image/svg+xml;base64,{0}", imagesrc);

Heres quick blazor playground which displays data uri for various image files

2
votes

A jpg file has a standard header. So even with different images I would expect the first few characters of the base64 to be the same.

When I convert a jpg to base64 it starts with "/9j/4AAQ"

So I think your data is not a (valid/complete) jpg file.