DeepWorks Studios Blazor Component Library

Demo user Administrator ยท Blazor Server Library 20261206222504-6e8ff32.dirty

DwsImage

The DwsImage component renders a single image from a URL, app asset, base64 string or binary source, with configurable fit/position, sizing, fallback and loading behavior. DwsImageGallery composes multiple DwsImage instances into a thumbnail gallery.

API Reference — DwsImage API Reference — DwsImageGallery

Basic / URL Source

A single image loaded from a URL, with a custom Alt.

Laptop showing code
Show Code
Image.razor

<DwsImage Name='Image_Basic' Src='https://images.unsplash.com/photo-1498050108023-c5249f4df085?w=1200&q=80' Alt='Laptop showing code' Width='320' Fluid='true' />
Image.razor.cs
// No additional code-behind is required for this example.

App Asset, Base64 & Binary Sources

The same image loaded three different ways: a bundled app asset, a base64 string, and raw binary data. Binary/base64 sources always take priority over Src.

App asset

App asset image

Base64 (1x1 pixel, Fit.Fill to visualize)

Base64 image

Binary stream (1x1 pixel, Fit.Fill to visualize)

Binary stream image
Show Code
Image.razor

<DwsImage Name='Image_AppAsset' Src='images/blackhole_black.png' SourceType='DwsImageSourceType.AppAsset' Alt='App asset image' Width='140' Height='140' Fit='DwsImageFit.Contain' />

<DwsImage Name='Image_Base64' Base64Data='@_base64Sample' SourceType='DwsImageSourceType.Base64' Alt='Base64 image' Width='140' Height='140' Fit='DwsImageFit.Fill' />

<DwsImage Name='Image_Binary' ImageStream='@_imageStreamSample' SourceType='DwsImageSourceType.Binary' Alt='Binary stream image' Width='140' Height='140' Fit='DwsImageFit.Fill' />
Image.razor.cs
// Built once and reused across renders: DwsImage reads a Stream source once, and a fresh
// instance handed in on every render would look like a new source each time.
private readonly byte[] _imageBytesSample = Convert.FromBase64String(_base64Sample);
private readonly MemoryStream _imageStreamSample;

public Image()
{
    _imageStreamSample = new MemoryStream(_imageBytesSample);
}

private const string _base64Sample =
    "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==";

Fit

Fit (None/Cover/Contain/Fill/ScaleDown) controls how the image resizes within its box. The logo sits on a light backdrop so it shows on the dark theme.

Fit.Cover

Image

Fit.Contain

Image
Show Code
Image.razor

<DwsImage Name='Image_FitCover' Src='images/blackhole_black.png' Style='background-color: #f4f4f4;' Fit='DwsImageFit.Cover'
          Width='160' Height='100' BorderWidth='1' BorderStyle='DwsBorderStyle.Solid' BorderColor='DwsColor.Secondary' />

<DwsImage Name='Image_FitContain' Src='images/blackhole_black.png' Style='background-color: #f4f4f4;' Fit='DwsImageFit.Contain'
          Width='160' Height='100' BorderWidth='1' BorderStyle='DwsBorderStyle.Solid' BorderColor='DwsColor.Secondary' />
Image.razor.cs
// No additional code-behind is required for this example.

Position

AlignHorizontal/AlignVertical position the image in the space a Contain or None fit leaves (a Fill or Cover image has none). Here it is aligned top-right.

Fit.Contain, aligned top-right

Image
Show Code
Image.razor

<DwsImage Name='Image_FitFillAligned' Src='images/blackhole_black.png' Style='background-color: #f4f4f4;' Fit='DwsImageFit.Contain'
          AlignHorizontal='DwsHorizontalAlignment.Right' AlignVertical='DwsVerticalAlignment.Top'
          Width='160' Height='100' BorderWidth='1' BorderStyle='DwsBorderStyle.Solid' BorderColor='DwsColor.Secondary' />

Sizing

Width, Height, MinWidth/MinHeight and MaxWidth/MaxHeight, all in pixels.

Image
Show Code
Image.razor

<DwsImage Name='Image_Sizing' Src='images/blackhole_black.png' Fit='DwsImageFit.Cover'
          MinWidth='80' MinHeight='80' MaxWidth='220' MaxHeight='220' Width='220' Height='220' />
Image.razor.cs
// No additional code-behind is required for this example.

Fallback Image

When Src fails to load, FallbackSrc is shown instead. Without a working fallback (or with an empty Src) the broken state is shown.

Image with fallback
Broken image, no fallback
Source and fallback both broken
Empty source
Show Code
Image.razor

<DwsImage Name='Image_Fallback' Src='images/does-not-exist.png'
          FallbackSrc='images/blackhole_black.png' Alt='Image with fallback' Style='background-color: #f4f4f4;'
          Width='160' Height='160' Fit='DwsImageFit.Cover' />

<DwsImage Name='Image_BrokenNoFallback' Src='images/does-not-exist.png' Alt='Broken image, no fallback'
          Width='160' Height='160' Fit='DwsImageFit.Cover' ShowLoadingIndicator='true' />

<DwsImage Name='Image_BothBroken' Src='images/does-not-exist.png' FallbackSrc='images/also-missing.png'
          Alt='Source and fallback both broken' Width='160' Height='160' ShowLoadingIndicator='true' />

<DwsImage Name='Image_EmptySrc' Src='' Alt='Empty source' Width='160' Height='160' ShowLoadingIndicator='true' />
Image.razor.cs
// No additional code-behind is required for this example.

Responsive (Fluid)

Fluid scales the image down to fit its container's width while preserving aspect ratio.

Fluid image
Show Code
Image.razor

<div style='max-width: 300px;'>
    <DwsImage Name='Image_Fluid' Src='https://images.unsplash.com/photo-1498050108023-c5249f4df085?w=1200&q=80' Alt='Fluid image' Fluid='true' />
</div>
Image.razor.cs
// No additional code-behind is required for this example.

srcset and sizes

SrcSet and Sizes let the browser pick the file that fits the screen (the network tab shows which width it asked for).

A laptop on a desk, served in the width the screen needs
Show Code
Image.razor

<div style='max-width: 480px;'>
    <DwsImage Name='Image_SrcSet'
              Src='https://images.unsplash.com/photo-1498050108023-c5249f4df085?w=960&q=80'
              SrcSet='https://images.unsplash.com/photo-1498050108023-c5249f4df085?w=480&q=80 480w, https://images.unsplash.com/photo-1498050108023-c5249f4df085?w=960&q=80 960w, https://images.unsplash.com/photo-1498050108023-c5249f4df085?w=1440&q=80 1440w'
              Sizes='(max-width: 520px) 100vw, 480px'
              Alt='A laptop on a desk, served in the width the screen needs'
              Fluid='true' AspectRatio='1.5' />
</div>

Lazy loading with a reserved box

Loading=Lazy defers an image outside the viewport until the user scrolls near it. AspectRatio reserves the image's box before the file has arrived, so the text under it does not jump - the image below is lazy, 3:2, and the paragraph after it keeps its place while it loads.

Lazy, with a reserved 3:2 box:

Code on a screen, loaded when it comes near the viewport

This paragraph is where it will stay: the box above had its height before the picture arrived.

Show Code
Image.razor

<div style='max-width: 480px;'>
    <DwsTypography Name='ImageDemo_Text07' Typography='DwsTypographyType.Paragraph' Class='small my-2' FontSize='0.875em'>Lazy, with a reserved 3:2 box:</DwsTypography>
    <DwsImage Name='Image_Lazy'
              Src='https://images.unsplash.com/photo-1461749280684-dccba630e2f6?w=960&q=80'
              Alt='Code on a screen, loaded when it comes near the viewport'
              Loading='DwsImageLoading.Lazy' Fluid='true' AspectRatio='1.5' />
    <DwsTypography Name='ImageDemo_Text08' Typography='DwsTypographyType.Paragraph' Class='small mt-2 mb-0' FontSize='0.875em'>This paragraph is where it will stay: the box above had its height before the picture arrived.</DwsTypography>
</div>

Image Gallery

DwsImageGallery composes DwsImage instances into a thumbnail strip with a main image; thumbnails are keyed so selecting one only updates the active thumbnail and the main image.

Show Code
Image.razor

<DwsImageGallery Name='ImageGallery_Demo' Items='@_galleryItems' @bind-SelectedIndex='_gallerySelectedIndex' />
Image.razor.cs
private int _gallerySelectedIndex;

private readonly List<DwsImageGalleryItem> _galleryItems =
[
    new DwsImageGalleryItem("https://images.unsplash.com/photo-1516117172878-fd2c41f4a759?w=1200&q=80", alt: "Nature"),
    new DwsImageGalleryItem("https://images.unsplash.com/photo-1498050108023-c5249f4df085?w=1200&q=80", alt: "Coding"),
    new DwsImageGalleryItem("https://images.unsplash.com/photo-1556740749-887f6717d7e4?w=1200&q=80", alt: "Coffee"),
    new DwsImageGalleryItem("https://images.unsplash.com/photo-1506748686214-e9df14d4d9d0?w=1200&q=80", alt: "Mountains")
];

Interface-Description

Name Typ Default Description
Src string? null Image URL, used for Url/AppAsset sources. Ignored when a base64/binary source is set.
SourceType DwsImageSourceType Auto Labels which source parameter is authoritative, for documentation purposes. Resolution is automatic.
Base64Data string? null A base64-encoded image, with or without a data: URI prefix.
ImageBytes byte[]? null Raw binary image data. Takes priority over Base64Data and Src.
ImageStream Stream? null A stream of raw binary image data, read once into memory. Takes priority over Base64Data and Src.
MimeType string "image/png" MIME type used to build a data: URI from ImageBytes/ImageStream.
Alt string? null Alternate text; falls back to a localized default.
Fit DwsImageFit? null (Contain) How the image resizes within its box (None/Cover/Contain/Fill/ScaleDown). Contain shows the whole image scaled into the given Width/Height.
AlignHorizontal DwsHorizontalAlignment Center Horizontal position of the image within its box.
AlignVertical DwsVerticalAlignment Center Vertical position of the image within its box.
Shape DwsShape Square Corner shape of the image.
BorderWidth int? null Border width in pixels.
BorderStyle DwsBorderStyle None Border line style.
BorderColor DwsColor Undefined Border color.
Width int? null Width in pixels.
Height int? null Height in pixels.
MinWidth int? null Minimum width in pixels.
MinHeight int? null Minimum height in pixels.
MaxWidth int? null Maximum width in pixels.
MaxHeight int? null Maximum height in pixels.
Margin int? null Outer margin in pixels.
Padding int? null Inner padding in pixels.
Fluid bool false Scales the image down to fit its container's width, preserving aspect ratio.
FallbackSrc string? null Shown in place of the resolved source if the image fails to load.
SrcSet string? null The srcset attribute: the picture in several widths or densities ('a-480.jpg 480w, a-960.jpg 960w' or 'logo.png 1x, logo@2x.png 2x'); the browser picks. Not rendered while FallbackSrc is shown or for data sources.
Sizes string? null The sizes attribute for a SrcSet with width descriptors ('(max-width: 600px) 100vw, 600px'). Rendered only together with SrcSet.
Loading DwsImageLoading DwsImageLoading.Auto Auto (no attribute), Lazy (fetched when the image comes near the viewport) or Eager.
AspectRatio double? null Width divided by height (1.778 for 16:9). Reserves the image's box before the file has loaded, so the layout does not shift. Ignored when both Width and Height are set.
IntrinsicWidth int? null The picture's width in pixels, written as the width ATTRIBUTE (no inline size). With IntrinsicHeight the browser reserves the box before the file arrives (no layout shift, CLS).
IntrinsicHeight int? null The picture's height in pixels, written as the height attribute. See IntrinsicWidth.
ShowLoadingIndicator bool true Whether a loading indicator is shown while the image downloads.
Wrapper bool true Renders the picture inside a positioned div that holds the loading indicator. False renders the bare img, valid inside a button or a link (a div is not); without the wrapper there is no loading indicator.
Name Typ Description
OnLoad EventCallback Fired when the image finishes loading.
OnError EventCallback Fired when the image fails to load.
Name Typ Default Description
Element ElementReference The associated web component. May be null if accessed before the component is rendered.
ComponentGuid Guid Unique identifier for the component.
TypeString string The name of the component type, obtained from reflection.
Authorized_RegisterEnabled bool false Indicates whether this component is enabled through a permission registry.
Authorized_RegisterSelected bool false Indicates whether this component is selected in permission selection mode.
GetInitializedState bool false Returns whether the component is initialized.
GetRenderModeState DwsRenderMode Returns the current render mode.
GetComponentState DwsComponentStateType Returns the current component state.
GetIsInteractiveState bool false Returns whether the component is interactive.
GetIsPrerenderingState bool false Returns whether the component is prerendering.
GetComponentLoadingState bool false Returns whether the component is loading.
IsFormBusy bool false Whether the DwsForm (or DwsFormBusyRegion) around the component is locked because its submit handler is running. The state itself is the cascaded FormBusyState (IDwsFormBusyState).
Name Typ Parameters Description
DebugInfo void Logs debug information about the component.
UpdateState void Updates the component's state and triggers StateHasChanged.
UpdateStateAsync Task Asynchronously updates the component's state and triggers StateHasChanged.
Dispose void Disposes the component and releases resources.
FocusAsync ValueTask bool containedInScrollableList Sets focus to the component's element.
Name Typ Default Description
Name string Required unique identifier for the component within its container.
Visible bool true Determines if the component is visible in the UI.
Disabled bool false Disables the form control, ensuring it doesn't participate in form submission.
ComponentNameSpace string The namespace for permission-based access control.
PermissionKey string? null A stable key that takes the place of Name in the generated permission namespace (Page.PermissionKey): the stored permission survives a rename, and components that share a key share one permission entry. Null: keyed by Name. Read once, when the component registers; an explicit ComponentNameSpace wins.
Class string Optional CSS class names.
Style string Optional in-line styles.
Tag object An optional user-defined object to store any additional data with the component.
BackReference DwsReference A reference to the enclosing component.
ID string The HTML ID attribute for the component.
AdditionalAttributes Dictionary<string, object> Additional HTML attributes for the component's root element.
IgnorePermissionChecks bool false Bypasses all permission checks for this component.
IgnorePermissionSelection bool false Ignore permission selection mode for this component.
IgnoreFormBusy bool false Takes the component out of the submit lock of the DwsForm (or DwsFormBusyRegion) around it: IsFormBusy stays false for it.
Name Typ Description
OnInitializedCallbackStarted EventCallback Callback when OnInitialized starts.
OnInitializedCallback EventCallback Callback during OnInitialized.
OnInitializedCallbackCompleted EventCallback Callback when OnInitialized completes.
OnInitializedAsyncCallbackStarted EventCallback Callback when OnInitializedAsync starts.
OnInitializedAsyncCallback EventCallback Callback during OnInitializedAsync.
OnInitializedAsyncCallbackCompleted EventCallback Callback when OnInitializedAsync completes.
OnIsComponentLoadingChanged EventCallback<bool> Callback when IsComponentLoading changes.
OnCurrentRenderModeChanged EventCallback<DwsRenderMode> Callback when CurrentRenderMode changes.
OnComponentStateChanged EventCallback<DwsComponentStateType> Callback when ComponentState changes.

Interface-Description

Name Typ Default Description
Items IEnumerable<DwsImageGalleryItem> [] The gallery's images (Src, ThumbnailSrc, Alt, FallbackSrc per item).
SelectedIndex int 0 Index of the selected item. Supports two-way binding via @bind-SelectedIndex.
ThumbnailFit DwsImageFit Cover Fit applied to thumbnail images.
MainFit DwsImageFit Contain Fit applied to the main image.
ThumbnailWidth int 80 Thumbnail width in pixels.
ThumbnailHeight int 80 Thumbnail height in pixels.
MainHeight int? 360 Main image height in pixels.
AriaLabel string? null Overrides the gallery's accessible label.
Name Typ Description
SelectedIndexChanged EventCallback<int> Fired when SelectedIndex changes. Used by @bind-SelectedIndex.
OnSelectedItemChanged EventCallback<DwsImageGalleryItem?> Fired with the newly selected item when the selection changes.
Name Typ Default Description
Element ElementReference The associated web component. May be null if accessed before the component is rendered.
ComponentGuid Guid Unique identifier for the component.
TypeString string The name of the component type, obtained from reflection.
Authorized_RegisterEnabled bool false Indicates whether this component is enabled through a permission registry.
Authorized_RegisterSelected bool false Indicates whether this component is selected in permission selection mode.
GetInitializedState bool false Returns whether the component is initialized.
GetRenderModeState DwsRenderMode Returns the current render mode.
GetComponentState DwsComponentStateType Returns the current component state.
GetIsInteractiveState bool false Returns whether the component is interactive.
GetIsPrerenderingState bool false Returns whether the component is prerendering.
GetComponentLoadingState bool false Returns whether the component is loading.
IsFormBusy bool false Whether the DwsForm (or DwsFormBusyRegion) around the component is locked because its submit handler is running. The state itself is the cascaded FormBusyState (IDwsFormBusyState).
Name Typ Parameters Description
DebugInfo void Logs debug information about the component.
UpdateState void Updates the component's state and triggers StateHasChanged.
UpdateStateAsync Task Asynchronously updates the component's state and triggers StateHasChanged.
Dispose void Disposes the component and releases resources.
FocusAsync ValueTask bool containedInScrollableList Sets focus to the component's element.
Name Typ Default Description
Name string Required unique identifier for the component within its container.
Visible bool true Determines if the component is visible in the UI.
Disabled bool false Disables the form control, ensuring it doesn't participate in form submission.
ComponentNameSpace string The namespace for permission-based access control.
PermissionKey string? null A stable key that takes the place of Name in the generated permission namespace (Page.PermissionKey): the stored permission survives a rename, and components that share a key share one permission entry. Null: keyed by Name. Read once, when the component registers; an explicit ComponentNameSpace wins.
Class string Optional CSS class names.
Style string Optional in-line styles.
Tag object An optional user-defined object to store any additional data with the component.
BackReference DwsReference A reference to the enclosing component.
ID string The HTML ID attribute for the component.
AdditionalAttributes Dictionary<string, object> Additional HTML attributes for the component's root element.
IgnorePermissionChecks bool false Bypasses all permission checks for this component.
IgnorePermissionSelection bool false Ignore permission selection mode for this component.
IgnoreFormBusy bool false Takes the component out of the submit lock of the DwsForm (or DwsFormBusyRegion) around it: IsFormBusy stays false for it.
Name Typ Description
OnInitializedCallbackStarted EventCallback Callback when OnInitialized starts.
OnInitializedCallback EventCallback Callback during OnInitialized.
OnInitializedCallbackCompleted EventCallback Callback when OnInitialized completes.
OnInitializedAsyncCallbackStarted EventCallback Callback when OnInitializedAsync starts.
OnInitializedAsyncCallback EventCallback Callback during OnInitializedAsync.
OnInitializedAsyncCallbackCompleted EventCallback Callback when OnInitializedAsync completes.
OnIsComponentLoadingChanged EventCallback<bool> Callback when IsComponentLoading changes.
OnCurrentRenderModeChanged EventCallback<DwsRenderMode> Callback when CurrentRenderMode changes.
OnComponentStateChanged EventCallback<DwsComponentStateType> Callback when ComponentState changes.
An error has occurred. This app may no longer respond until reloaded.