Recursive AI

Render CSS background images 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.

Supply tile.png (the checkerboard in the zip) for the CSS background. OneRender doesn’t fetch files or run JavaScript.

Terminal
dotnet add package RecursiveAI.OneRender.Image

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneRender.Image;
 
string html = """
<body style="margin:0">
<div style="height:48px;
  background-image:url(tile.png)">
</div>
""";
string root = "https://example.com/";
byte[] tile = File.ReadAllBytes("tile.png");
var image = HtmlRender.ToImage(html,
    new HtmlRenderOptions {
        Width = 96, Height = 48,
        BaseUrl = root,
        Resources = url =>
            url == root + "tile.png"
                ? tile : null
    });
File.WriteAllBytes(
    "pattern.png", image.ToPng());
string size = $"{image.Width}x{image.Height}";
Console.WriteLine($"pattern.png: {size}");

https://example.com/ is only a lookup key. Nothing is fetched.

Unzip the example, then dotnet run.

Text
pattern.png: 96x48

Teal and pale-blue checkerboard tiled across the PNG

pattern.png shows six tiles across and three down. If you see a blank box, check that tile.png is beside the project and that Resources matches the resolved URL.

Variations

Center a single tile. Replace the <div> in the HTML, keeping the same viewport and resource callback:

HTML
<div style="height:48px;background-image:url(tile.png);
  background-repeat:no-repeat;background-position:center">
</div>

The short answer

Render the HTML with HtmlRender.ToImage and save the result with ToPng(). For a CSS background-image that uses a URL, set HtmlRenderOptions.Resources to return the image bytes. Set BaseUrl if the CSS has a relative URL. The example maps the resolved URL https://example.com/tile.png to a file it reads locally; that address is just a lookup key.

Why is my CSS background image not showing?

Check the address passed to Resources. With a base of https://example.com/, the inline url(tile.png) resolves to https://example.com/tile.png, not tile.png. The callback returns null for unknown addresses. The renderer won’t make a network request or look up the image in the working directory on its own; the C# code reads the file and provides the bytes.

Then check the size of the box. A CSS background paints inside an existing box; it does not give an empty <div> a height. That’s why the example gives the <div> a height and removes the body margin. If the box falls outside the viewport, increase Width or Height; the image is a capture of that viewport, not of the entire document.

How do I repeat or resize a CSS background image in a PNG?

Pick the result you want before changing the viewport. Leaving the background size alone keeps the checkerboard at its original size; the default repeat fills the box. The variation makes a single copy and positions it in the center. For a photo that must fill a banner, use background-size: cover with background-repeat: no-repeat; the box may crop the photo. Use contain instead when the whole photo has to fit, and choose a background color for any space it leaves.

The OneRender architecture guide describes “One painted layer per background-image entry,” so you can also combine an image with a gradient on the same box. Keep the CSS list order in mind: the first image is on top of later images and the background color.

All onerender recipes