Images

Display an image as a thumbnail in the grid, and larger in a display field

Introduction

Images can be displayed using the ImageDisplay:

using StellarAdmin.Resources.Displays;

columns.Add(destination => destination.HeroImageUrl, column => column.UseDisplay<ImageDisplay>());
The destinations grid with a thumbnail of each destination's hero image

In the grid, ImageDisplay displays a thumbnail, and in a display field, a larger image. When users click the image, StellarAdmin opens a larger version in a popup.

When the value is empty, ImageDisplay displays a placeholder. When the image fails to load, it displays a broken image.

A display field with no image, and with an image that fails to load: Empty
Empty
A display field with no image, and with an image that fails to load: Broken
Broken

Constructing the image URL

Typically, fields referencing images do not store the full image URL, but just a filename or other reference to the image that can be resolved to a full URL. To help StellarAdmin resolve the full URL, use the ResolveUrl() method.

columns.Add(destination => destination.HeroImageUrl, column =>
    column.UseDisplay<ImageDisplay>(image =>
        image.ResolveUrl(value => $"/photos/{value}")));

If you need a service to resolve the URL, you can use the ResolveUrl(async (value, services, cancellationToken) => ...) overload which allows you to resolve the service via DI using the services parameter.

The grid calls ResolveUrl() for every row, so keep it fast: build the URL from the value rather than looking it up in a database or calling an API.

Setting the size and shape

You have complete control over the size and aspect ration of the image. The example below displays a small, landscape image, with a larger version when users hover over it:

using StellarAdmin.UI;

resource.AllowView(view => view.Fields(fields =>
{
    fields.Add(destination => destination.Name);
    fields.Add(destination => destination.HeroImageUrl, field =>
        field.UseDisplay<ImageDisplay>(image =>
        {
            image.Size = ImageDisplaySize.ExtraSmall;
            image.AspectRatio = ImageAspectRatio.Landscape;
            image.Interaction = ImageDisplayInteraction.Preview;
        }));
}));
Hovering over a small, landscape hero image on a destination's view page displays a larger version

ImageDisplay has the following members that control image size, shape, and interactivity:

MemberDescription
SizeSets the image's height in the grid, and its maximum height in a display field: ImageDisplaySize.ExtraSmall, Small, Medium or Large. The default is Small (32px) in the grid, and Medium (15rem) in a display field.
AspectRatioSets the image's shape: ImageAspectRatio.Square, Landscape, Widescreen, Portrait or Original. The default is Square in the grid, and Original in a display field.
FitSets how the image fills its shape: ImageFit.Cover crops the image, and Contain fits all of it. The default is Cover.
InteractionSets what happens when people interact with the image: ImageDisplayInteraction.Lightbox opens a larger version on click, Preview displays a larger version on hover, and None does nothing. The default is Lightbox.

On this page