---
title: "Render a Razor component to PNG in C#"
url: https://recursiveai.org/onerender/cookbook/razor-to-png/
description: "Render a Blazor Razor component to a PNG in C#. Set the image width and height and save the result without launching a browser. Includes a runnable .NET example."
version: "1.4.0"
---

# Render a Razor component to PNG in C#

**OneRender** is a free, open-source HTML and CSS rendering library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneRender.Razor) · [GitHub](https://github.com/recursive-ai-oss/onerender-csharp) · [Docs](https://recursiveai.org/onerender/docs/) · [Download example (.zip)](https://recursiveai.org/onerender/cookbook/razor-to-png/razor-to-png.zip)

Use `RenderImageAsync` to save a Blazor component as PNG. No browser or JavaScript.

```shell
dotnet add package RecursiveAI.OneRender.Razor
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (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.

```csharp
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");
```

```text
Wrote card.png
```

If 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`:

```razor
@namespace RazorCard
<h1 style="color: #235; font-size: 32px">
    Order ready
</h1>
```

Unzip [the example](https://recursiveai.org/onerender/cookbook/razor-to-png/razor-to-png.zip), then `dotnet run`.

## Variations

**Double the output pixels without changing the layout.** Replace the render call in `Program.cs` with:

```csharp
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

- [Convert HTML to PNG in C#](https://recursiveai.org/onerender/cookbook/html-to-png/)
- [Render a Razor component to PDF in C#](https://recursiveai.org/onerender/cookbook/razor-to-pdf/)

---

## 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:

```razor
@namespace RazorCard
@using Microsoft.AspNetCore.Components
<h1>@Message</h1>

@code {
    [Parameter]
    public string Message { get; set; } = "";
}
```

Then replace the render call in `Program.cs`:

```csharp
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](https://recursiveai.org/onerender/docs/razor/) for asset resolution details.
