Blazor Image is a powerful image optimization library for Blazor that automates compression, responsive sizing, and caching of static images (.jpg, .png, .webp, .avif)βall in one component.
Easily deliver optimized images with a single line:
<Image Src="/images/sample.jpg" Alt="Descriptive alt text" Width="300" Height="200" Priority="true" />βοΈ If BlazorImage helped you, please star it on GitHub to support visibility and development.
- π¦ Optimized Images β Compress JPEG, PNG, WebP, and AVIF with up to 90% size reduction.
- π€ Lazy Loading + Placeholders β Enhance performance and loading speed.
- πΌ Flexible Formats β Output WebP, JPEG, PNG, or AVIF.
- β‘ Smart Caching β Efficient long-term caching with auto revalidation.
- π± Responsive Support β Define sizes/aspect ratios for adaptive layouts.
- βΏ Accessible & SEO-Friendly β Supports alt text, captions, semantics.
- π§ Interactive States β Enable state-aware behavior via
EnableInteractiveState.
- .NET 8.0 SDK or later
- Blazor Server supported (Blazor WebAssembly not yet)
π Supports local images only. Remote support coming soon.
dotnet add package BlazorImage --version 1.0.5Or add manually:
<PackageReference Include="BlazorImage" Version="1.0.5" />In _Imports.razor:
@using BlazorImageIn Program.cs:
builder.Services.AddBlazorImage();configure BlazorImage options within the AddBlazorImage method:
builder.Services.AddBlazorImage(options =>
{
// Output directory for optimized images (relative to wwwroot). Default: "_optimized"
options.OutputDir = "Path";
// Responsive widths for generated image variants.
// Recommended: [xs, sm, md, lg, xl, 2xl, ...] to cover common breakpoints.
// Default: [480, 640, 768, 1024, 1280, 1536]
options.Sizes = [640, 1024, 1280];
// Default image quality (15β100). Lower means more compression.
// Default: 75 | Recommended: 70β80 (Good balance of quality and file size)
options.DefaultQuality = 70;
// Default image output format (jpeg, png, webp, avif).
// Default: webp | Recommended: FileFormat.webp (widely supported and efficient)
options.DefaultFileFormat = FileFormat.webp;
});Map required middleware in:
app.MapBlazorImageRuntime();In App.razor:
<link rel="stylesheet" href="AssemblyName.styles.css" />
<script src="_content/BlazorImage/BlazorImage.min.js"></script>#if DEBUG // Recommended to enable only in development
app.MapBlazorImageDashboard("/blazor-image-admin"); // Choose your preferred path
#endif
β οΈ Important: UseCssClassinstead ofclass, andStyleinstead ofstylefor all styling.
Use the <Image> component to render optimized, responsive, and accessible images:
<Image Src="/images/sample.jpg" Alt="Descriptive alt text" Width="300" Height="200" />-
Src(required):
The path to the original image file. BlazorImage will handle the optimization. -
Alt(required):
Alternative text for the image, crucial for accessibility and SEO. -
Width,Height(optional,int?):
Set fixed image dimensions in pixels. -
Fill(optional,bool):
Iftrue, the image fills its container while maintaining aspect ratio. Defaults tofalse.AspectRatio(optional,(int, int)):
Used whenFill="true"to preserve image ratio.
-
PriorityEager load image (optional,bool):
Iftrue, the image loads eagerly. Defaults tofalse. -
DefaultSrc(optional,string):
Fallback image to override the error messages. -
Quality(optional,int?):
Sets optimized image quality (15β100). Defaults to library setting. -
Format(optional,FileFormat?):
Desired image output format (webp,jpeg,png,avif, etc.).
Note: AVIF may require extra build steps in some environments. -
Sizes(optional,string):
Sets thesizesattribute for responsive behavior. -
CssClass(optional,string):
CSS classes to apply to the<img>tag.
Use this instead ofclass. -
Style(optional,string):
Inline styles for the<img>tag.
Use this instead ofstyle. -
Caption(optional,string):
Text to display below the image. -
CaptionClass(optional,string):
CSS class for styling the caption. -
EnableInteractiveState(optional,bool):
Enables state interactivity for the image (e.g., loading, error state handling). -
Id(optional,string):
Custom ID for the image element. Used internally or for anchoring, preload. -
AdditionalAttributes(optional):
Add any additional HTML attributes via aDictionary<string, object>.
These are applied directly to the<img>element.
Responsive with fill:
<div style="width: 400px; height: 400px;">
<Image Src="/images/banner.jpg" Alt="Banner" Fill="true" />
</div>Static dimensions:
<Image Src="/images/logo.png" Alt="Logo" Width="100" Height="100" />Eager loading (hero image):
<Image Src="/images/hero.jpg" Alt="Hero image" Priority="true" />Eager loading with preload:
<HeadContent>
<SectionOutlet SectionName="hero-image-id" /> <!-- Set Image Id -->
</HeadContent>
<Image
Id="hero-image-id" <!-- Add a unique image id -->
Priority="true" <!-- Enables priority loading and preload -->
Src="/hero-image.jpg"
Alt="Hero image"
Width="1200"
Height="600" />Custom format + quality:
<Image Src="/images/preview.png" Alt="Preview" Format="FileFormat.png" Quality="85" />Sizes Attribute:
<Image Src="/images/avatar.jpg" Alt="Avatar image" Sizes="(max-width: 768px) 8rem, 13rem" CssClass="rounded-full object-cover" Fill="true" Priority="true" />Aspect Ratio:
<Image Src="/images/landscape.jpg" Alt="Landscape" AspectRatio="@(16,9)" Fill="true" />Adding a caption:
<Image Src="/images/product.jpg" Alt="Product shot" Caption="The latest product" CaptionClass="product-caption" />Run the BlazorImage.Demo app to see examples in action.
- Clone the repository
- Install .NET 9.0 SDK or newer
- Build the solution
- Set
BlazorImage.Demoas startup project - Press F5 or run
Ensure
wwwroothas sample images for demo display.
MIT License. See LICENSE.txt for details.
blazor, image optimization, responsive images, lazy loading, webp, avif, jpeg, dotnet, blazor component, caching, middleware, seo, accessibility