Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display an Image that is stored in DB as byte[] by a <img src="?" /> in Blazor

I want to show an image that is stored in the databese, I have this function in my DAL that I inject to the component with the name admin:

 public byte[] GetCategoryLabelImage(int categoryLabelID) 
    {
        var categoryLabel = context.CategoryLabels.FirstOrDefault(cl => cl.Id == categoryLabelID);
        byte[] image = categoryLabel?.Image?.Image;
        return image;        
    }

But when I use an tag like this:

<img src="@admin.GetCategoryLabelImage(label.Id)" />

It shows nothing, Is this the correct way to display an image?

like image 586
mz1378 Avatar asked Aug 13 '19 15:08

mz1378


People also ask

How do I display an image in Blazor?

To display a static image in the Blazor component, first, it must store the image in any folder under the wwwroot file. It can be accessed by the relative path. To serve a static image, you need to use the app. UseStaticFiles () method in the start.

Where to store images in blazor project?

Place the images in a new folder named images in the app's web root ( wwwroot ). The use of the images folder is only for demonstration purposes. You can organize images in any folder layout that you prefer, including serving the images directly from the wwwroot folder.


1 Answers

I solved it as follows:

<img src="data:image/bmp;base64, @(Convert.ToBase64String(admin.GetCategoryLabelImage(label.Id)))"/>
like image 190
mz1378 Avatar answered Sep 28 '22 09:09

mz1378