Use RenderImageAsync to save a Blazor component as PNG. No browser or JavaScript.
dotnet add package RecursiveAI.OneRender.RazorRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
The zip’s project file uses the Web SDK (Microsoft.NET.Sdk.Web). Card comes from Card.razor below.
using OneRender.Razor; using RazorCard; var builder = WebApplication.CreateBuilder(); builder.Services.AddOneRender(); await using var app = builder.Build(); var renderer = app.Services .GetRequiredService<RazorRenderer>(); var card = await renderer .RenderImageAsync<Card>(options: new() { Width = 480, Height = 240 }); File.WriteAllBytes("card.png", card.ToPng()); Console.WriteLine("Wrote card.png");
Wrote card.pngIf an image or stylesheet can’t be found, the renderer still makes the PNG; check card.MissingAssets for the missing paths.
The PNG is 480 × 240 pixels. Save this component as Card.razor next to Program.cs:
@namespace RazorCard <h1 style="color: #235; font-size: 32px"> Order ready </h1>
Unzip the example, then dotnet run.
Variations
Double the output pixels without changing the layout. Replace the render call in Program.cs with:
var card = await renderer.RenderImageAsync<Card>( options: new() { Width = 480, Height = 240, DevicePixelRatio = 2 });
With the same save call, card.png is now 960 × 480 pixels.
Related recipes
The short answer
Register AddOneRender(), then call RenderImageAsync<TComponent> with a width and height. Encode the result with ToPng() and save those bytes. In this example the viewport is 480 × 240 CSS pixels and the default pixel ratio is 1, so the saved PNG is 480 × 240 pixels.
How do I save a Blazor component as a PNG?
Pass the compiled component type, not its URL or the path to its .razor file. OneRender runs that component through ASP.NET Core’s HtmlRenderer, then lays out and paints the resulting HTML and CSS. It wraps a fragment like Card in an HTML document for you. The example builds the host to get the renderer from dependency injection; it never calls app.Run(), so there’s no HTTP request or browser tab. This captures the component’s server-rendered markup, not the state of a component already open in a user’s browser.
For a thumbnail or card with a fixed size, choose a viewport large enough for the text and margins. Contents outside it are clipped. Use DevicePixelRatio when you want more output pixels at the same layout size, as in the variation. To fit another line of text, increase the viewport height instead.
How do I pass data to a Blazor component when rendering it to an image?
The sample uses fixed text. To pass a value such as an order number, declare a [Parameter] property on your component and supply a dictionary whose key is that property’s name. Each call uses a fresh service scope, so scoped dependencies are resolved for that render rather than shared across renders.
For example, replace Card.razor with this version:
@namespace RazorCard @using Microsoft.AspNetCore.Components <h1>@Message</h1> @code { [Parameter] public string Message { get; set; } = ""; }
Then replace the render call in Program.cs:
var parameters = new Dictionary<string, object?> { ["Message"] = "Order 1042 is ready" }; var card = await renderer.RenderImageAsync<Card>( parameters, new() { Width = 480, Height = 240 });
The example needs no asset files because its CSS is inline. For a component with linked images, stylesheets or fonts, the renderer checks the app’s web root and static web assets. If a PNG comes out unstyled, look at MissingAssets before changing the viewport: an unresolved stylesheet path won’t be fixed by a larger canvas. In this version, use inline <svg> for SVG artwork; an external SVG in an <img> isn’t drawn. See the Razor rendering docs for asset resolution details.