I am trying show images in a Blazor view, but it's not working and shows only an image icon:
<img src="~/Images/watch.jpg" asp-append-version="true" width="300px" />
Here my image resides in this path wwwroot/Images/watch.jpg
I am trying show images in a Blazor view, but it's not working and shows only an image icon:
<img src="~/Images/watch.jpg" asp-append-version="true" width="300px" />
Here my image resides in this path wwwroot/Images/watch.jpg
You are probably running into this bug: https://github.com/aspnet/Blazor/issues/1216 and need to remove the ~ to make it work, or wait for the 0.6 release.
I have found for everything in blazor '../Images' is what you need.
If you do anything with images / css in Blazor, you might like this project called BlazorStyled by Chanan:
https://github.com/chanan/BlazorStyled
I recently built a sample project and tutorial with it called Blazor Image Gallery, that also uses my Nuget package DataJuggler.Blazor.FileUpload.
Here is one of the components called an ImageButton:
@using BlazorStyled
<Styled @bind-Classname="@ImageStyle">
background-image: url('@ImageUrl');
width: @WidthPixels;
height: @HeightPixels;
transform: scale(@Scale);
transform-origin: left;
border: 2px solid black;
z-index: @ZIndex;
outline: none;
position: absolute;
left: @ColumnLeftPixels;
top: @RowTopPixels;
display: inline-block;
</Styled>
<button class="@ImageStyle" @onclick="Button_Clicked"></button>
My ImageUrl property comes from my SQL Images table after a user uploads a file:
ImageUrl = '../Images/Gallery/Christina/Image1.08b2bb51-5.png'
Then in the usage I iterate my list of images for the selected artist and set the image on each button.
<div class="galleryimages">
@if (SelectedArtist.HasImages)
{
@foreach (Image image in SelectedArtist.Images)
{
<ImageButton Image=image Parent=this></ImageButton>
}
}
</div>
Full source code and video:
Sample Project: https://github.com/DataJuggler/BlazorImageGallery
Read thisenter link description here and you'll understand how to solve your problem. Basically, you have to tell the framework to use the root directory, because by default it is searching for the file in the wrong place - http:///images/.