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?
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.
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.
I solved it as follows:
<img src="data:image/bmp;base64, @(Convert.ToBase64String(admin.GetCategoryLabelImage(label.Id)))"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With