Image¶
The Image component displays an image on a web page. It may seem strange that we have an Image component when there already is an HTML <img> element and Blazor has features that enable C# interactions with that image, but we need to activate other features that were once present in Web Forms, such as DescriptionUrl for accessibility, ImageAlign for legacy alignment, and GenerateEmptyAlternateText for accessibility compliance.
Original Microsoft documentation: https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.image?view=netframework-4.8
Features Supported in Blazor¶
AlternateText- alternate text displayed when the image is unavailable (renders asaltattribute)DescriptionUrl- the location to a detailed description for the image (renders aslongdescattribute)GenerateEmptyAlternateText- whentrue, generates an emptyalt=""attribute for decorative images (accessibility compliance)ImageAlign- the alignment of the image in relation to other elements on the Web page (renders asalignattribute)ImageUrl- the URL of the image to display (renders assrcattribute)ToolTip- tooltip text displayed on hover (renders astitleattribute)Visible- controls image visibility
Blazor Notes¶
- The
ImageUrlproperty maps directly to the HTMLsrcattribute. In Blazor, you can use relative paths from yourwwwrootfolder (e.g.,/images/logo.png) or absolute URLs - For static assets in Blazor, place images in the
wwwrootfolder and reference them with a leading/ - The
ImageAlignproperty uses theImageAlignenum with values:NotSet,Left,Right,Baseline,Top,Middle,Bottom,AbsBottom,AbsMiddle,TextTop - The
longdescattribute (fromDescriptionUrl) is considered obsolete in HTML5 but is rendered for backward compatibility. Consider usingaria-describedbyin new applications
Web Forms Features NOT Supported¶
- Style properties (
BackColor,ForeColor,BorderColor,BorderStyle,BorderWidth,CssClass,Width,Height,Font) - Not yet implemented; use standard HTML/CSS styling on a wrapper element - AccessKey - Not supported; use HTML
accesskeyattribute directly if needed - TabIndex - Not supported; use HTML
tabindexattribute directly if needed - EnableTheming/SkinID - Not supported; ASP.NET theming is not available in Blazor
- EnableViewState - Not needed; Blazor manages state differently
- Lifecycle events (
OnDataBinding,OnInit,OnLoad, etc.) - Not supported; use Blazor lifecycle methods instead
Web Forms Declarative Syntax¶
<asp:Image
AccessKey="string"
AlternateText="string"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
Inset|Outset"
BorderWidth="size"
CssClass="string"
DescriptionUrl="uri"
Enabled="True|False"
EnableTheming="True|False"
EnableViewState="True|False"
ForeColor="color name|#dddddd"
GenerateEmptyAlternateText="True|False"
Height="size"
ID="string"
ImageAlign="NotSet|Left|Right|Baseline|Top|Middle|Bottom|
AbsBottom|AbsMiddle|TextTop"
ImageUrl="uri"
OnDataBinding="DataBinding event handler"
OnDisposed="Disposed event handler"
OnInit="Init event handler"
OnLoad="Load event handler"
OnPreRender="PreRender event handler"
OnUnload="Unload event handler"
runat="server"
SkinID="string"
Style="string"
TabIndex="integer"
ToolTip="string"
Visible="True|False"
Width="size"
/>
Blazor Razor Syntax¶
Basic Image¶
Image with Tooltip¶
Image with Alignment¶
@using BlazorWebFormsComponents.Enums
<Image ImageUrl="/images/icon.png"
AlternateText="Icon"
ImageAlign="ImageAlign.Left" />
<p>This text flows around the left-aligned image.</p>
Decorative Image (Empty Alt Text)¶
Image with Description URL¶
<Image ImageUrl="/images/complex-chart.png"
AlternateText="Sales Chart"
DescriptionUrl="/descriptions/sales-chart.html" />
HTML Output¶
Blazor Input:
<Image ImageUrl="/images/photo.jpg"
AlternateText="A photo"
ToolTip="Click to enlarge"
ImageAlign="ImageAlign.Left" />
Rendered HTML:
Migration Notes¶
When migrating from Web Forms to Blazor:
- Remove
asp:prefix - Change<asp:Image>to<Image> - Remove
runat="server"- Not needed in Blazor - Remove
IDattribute - Use@refif you need a reference to the component - Update image paths - Place images in
wwwrootfolder and use paths like/images/photo.jpg - Add
@usingfor enums - Add@using BlazorWebFormsComponents.Enumswhen usingImageAlign - Style properties - Wrap the Image in a
<div>or<span>and apply CSS for styling not yet supported
Before (Web Forms)¶
<asp:Image ID="imgLogo"
ImageUrl="~/images/logo.png"
AlternateText="Company Logo"
CssClass="logo-image"
runat="server" />
After (Blazor)¶
Examples¶
Basic Image Display¶
Dynamic Image URL¶
<Image ImageUrl="@imageUrl" AlternateText="@imageAlt" />
@code {
private string imageUrl = "/images/default.png";
private string imageAlt = "Default image";
void ChangeImage()
{
imageUrl = "/images/alternate.png";
imageAlt = "Alternate image";
}
}
Conditional Visibility¶
<Image ImageUrl="/images/banner.jpg"
AlternateText="Promotional Banner"
Visible="@showBanner" />
<button @onclick="ToggleBanner">Toggle Banner</button>
@code {
private bool showBanner = true;
void ToggleBanner()
{
showBanner = !showBanner;
}
}
Text Wrapping with Aligned Image¶
@using BlazorWebFormsComponents.Enums
<Image ImageUrl="/images/author.jpg"
AlternateText="Author photo"
ImageAlign="ImageAlign.Left" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
This text wraps around the left-aligned image. The ImageAlign
property allows you to control how content flows around the image.</p>
<div style="clear: both;"></div>
See Also¶
- ImageButton - A clickable image that acts as a button
- HyperLink - Wrap an Image in a HyperLink for clickable images
- Live Demo - Interactive Image samples