Recursive AI

Render inline SVG in HTML 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.

Render SVG embedded in HTML, not referenced by an img element.

Terminal
dotnet add package RecursiveAI.OneRender.Image

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

C#
using OneRender.Image;
var image = HtmlRender.ToImage("""
<svg width="80" height="80">
<circle cx="40" cy="40" r="30"
        fill="blue"/>
</svg>
""");
File.WriteAllBytes("inline-svg.png", image.ToPng());
Console.WriteLine("Saved inline-svg.png");
Text
Saved inline-svg.png

Unzip the example, then dotnet run.

The full program

All HTML and CSS are embedded in Program.cs; no input files are needed.

C#
using OneRender.Image;
 
string html = """
<body style="margin:0">
<svg width="120" height="80" viewBox="0 0 120 80">
  <rect width="120" height="80" fill="#eef2ff"/>
  <circle cx="60" cy="40" r="28" fill="#0066cc"/>
  <path d="M45 40 L56 51 L77 28" fill="none"
        stroke="white" stroke-width="6"/>
</svg>
</body>
""";
var image = HtmlRender.ToImage(html,
    new HtmlRenderOptions { Width = 120, Height = 80 });
File.WriteAllBytes("inline-svg.png", image.ToPng());
Console.WriteLine($"inline-svg.png: {image.Width}x{image.Height}");
Text
inline-svg.png: 120x80

The full program writes a blue circular badge with a white check mark on a pale rectangle. Invalid SVG can be omitted by the rasterizer; the image convenience API does not return diagnostics.

Variations

Replace the full program’s image options to produce twice as many pixels without changing the CSS layout:

C#
new HtmlRenderOptions
{
    Width = 120, Height = 80, DevicePixelRatio = 2
}
Text
inline-svg.png: 240x160

The short answer

Place the <svg> element directly in your HTML string and call HtmlRender.ToImage. Encode the result with ToPng(). The example uses a rectangle, a circle and a stroked path, with all colors and geometry in the markup. No resource callback or downloaded artwork is needed.

OneRender paints inline SVG as an image at the SVG element’s laid-out size. The surrounding document still goes through HTML layout, so an icon can sit beside headings or other content. This recipe is an HTML rendering operation, not a general file-conversion API for arbitrary SVG documents.

How do I render HTML SVG to PNG in C#?

Keep the vector markup inline, set explicit SVG dimensions and provide a viewport large enough to contain it. In the full program, the SVG is 120 by 80 CSS pixels and the image viewport has the same dimensions. Removing the body margin prevents an offset from moving the badge partly outside the image.

The viewBox describes the coordinate system used by the shapes. OneRender sizes the SVG to its layout box and keeps viewBox and preserveAspectRatio. Set those attributes intentionally when an icon must scale into a different box rather than assuming that its path coordinates are output pixels.

The SVG is self-contained: it does not refer back into the surrounding HTML page. Put the needed shape definitions and SVG styling inside it. Explicit fill and stroke attributes make this badge independent of outside style rules. The backend also supplies the SVG item’s currentColor, but this example does not need inherited color to draw its check mark.

How do I render SVG to PNG at 2x?

Set DevicePixelRatio = 2 as in the variation. Layout stays 120 by 80 CSS pixels, while the resulting raster is 240 by 160 pixels. Changing the density is useful when the image will be displayed at its original CSS size on a high-density screen. Increasing the viewport instead would change the available layout space, which is a different operation.

Rasterization is bounded. OneRender limits each SVG image to 8,192 px per side and 16,777,216 px in total. The display list also limits serialized SVG markup to 1,048,576 characters. Raising density can therefore make an otherwise small layout exceed a raster limit. Keep icons sized for their intended use rather than rendering an oversized intermediate image.

Can I render inline SVG to PNG for free?

Yes. The linked NuGet package is free and open source under MIT or Apache-2.0. The example runs in your .NET application, not through an upload service. It uses no remote inputs or credentials.

Inline markup matters here: an SVG referenced by an HTML img element is not drawn in this release. Do not turn the snippet into <img src="icon.svg"> and expect the same result. Inline the supported drawing instead. JavaScript is not executed, so a charting script that creates SVG must run elsewhere before you supply the resulting markup. Finally, inspect the saved image when adapting complex artwork: the image convenience API supplies pixels, not a list of SVG rendering warnings.

All onerender recipes