Blazor - Displaying an Image

I wanted to be able to display an image file within a Blazor page.  During my research I realized that there is very little difference between displaying an image in Blazor as compared to displaying it in a HTML page.  So, I decided to add a couple of other features to my application.  

The features I have added are: 
  1. 1. Get a list of files on the server 
  1. 2. Display at list in a Blazor page 
  1. 3. Provide a UI effect to zoom in on the image 

I will be using the Blazor Server configuration for this application.  We can do this with the Client-side configuration, but you need an API to access the server files.  I have created a folder under WWWROOT called DsiplayImages where I have placed several images I will be working with.  I have created a service that will access the image files from the server. 

In addition, I have created a model for the file properties I a need. 
    public class ImageSpec 
    { 
        public string Name { getset; } 
        public string DisplayURL { getset; } 
    } 


I could have just used the FileInfo object, but I needed a clean URL to display the image, I will explain that soon. 

To get the collection of files I was able to simply use the C# DirectoryInfo object call: 

    DirectoryInfo d = new DirectoryInfo(@"wwwroot\" + imagePath); 
    var files = d.GetFiles("*.*"); 
    if (files != null) 
    { 
        foreach (var f in files) 
        { 
            var spec = new ImageSpec 
            { 
                 Name = Path.GetFileNameWithoutExtension(f.Name), 
                 DisplayURL = $"{imagePath}\\{f.Name}" 
            }; 
            result.Add(spec); 
        } 
    }

If you notice you must include a path of “WWWROOT” plus the image path.  It is noteworthy that we don’t have to use the full path or the ServerPath to access the files.  This is because we are running on the server and it references the base server path for us.

When building the Display URL, note we don’t need the server oath or even the “WWWROOT” part of the path.  This is because ASP.Net Core will use the static file middleware to retrieve the file from “WWWROOT”. 

For the HTML page we add an OnInitiliazedAsync Method to get the files on the page load.  We do display a “Loading…” message while we retrieve the images. 

Once the file collection has been populated, we use a foreach loop to set the HTML image tag. 

@if (files == null) 
{ 
    <p><em>Loading...</em></p> 
} 
else 
{ 

    @foreach (var file in files) 
    { 
        <img src="@file.DisplayURL" alt="@file.Name" class="image-display"> 
    } 

} 

@code { 
    List<ImageSpec> files; 
     protected override async Task OnInitializedAsync() 
     { 
         files = await ImageManager.GetFileList(); 
     } 
} 

There is an additional attribute you can use with the image tag, “asp-append-version”.  The attribute will refresh the image if the file has changed instead of using the cached one. 
Now that we are displaying the images, lets add a zoom effect.  As I looked in how to implement a zoom effect, I found that CSS is the easiest and common choice.  I added a HOVER style to show this effect.  As the user hovers, the size changes. 

.image-display { 
    height : 42px; 
    width : 42px; 
} 

.image-display:hover { 
    height542px; 
    width542px; 
} 

This was a simple solution and it worked.  The user experience could be improved, but I will leave that for later. 

As you can see displaying an image in Blazor is straight forward.  The key is to understand the pathing to get the images on the server and from the HTML page. 
  

Comments

Popular posts from this blog

Yes, Blazor Server can scale!

Blazor new and improved Search Box

Blazor Wizard Step Component