---
title: "Render CSS background images to PNG in C#"
url: https://recursiveai.org/onerender/cookbook/render-css-background-images-to-png/
description: "Render HTML and its CSS background-image to a PNG in C#. Resolve a local image with OneRender and choose whether to tile, center, or scale it."
version: "1.4.0"
---

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

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

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

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

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onerender/cookbook/render-css-background-images-to-png/render-css-background-images-to-png.zip), then `dotnet run`.

```text
pattern.png: 96x48
```

![Teal and pale-blue checkerboard tiled across the PNG](https://recursiveai.org/onerender/images/render-css-background-images-to-png/pattern.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>
```

## Related recipes

- [Convert HTML to PNG in C#](https://recursiveai.org/onerender/cookbook/html-to-png/)
- [Render CSS gradients to PNG in C#](https://recursiveai.org/onerender/cookbook/render-css-gradients-to-image/)

## 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](https://recursiveai.org/onerender/docs/architecture/#paint-paintingpaintercs) 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.
