Convert SVG to PNG in C#

To convert SVG to PNG in C#, load the SVG document or open the SVG file as a stream, create ImageSaveOptions, and call Converter.ConvertSVG() with the source, options, and output path or stream provider. PNG is the default ImageSaveOptions.Format value.

PNG is a lossless raster format that preserves sharp edges, text, icons, diagrams, and transparency-friendly output. SVG to PNG conversion is useful when vector graphics must be rendered as images for previews, thumbnails, UI assets, or systems that cannot consume SVG directly.

Before running the examples, install and configure Aspose.HTML for .NET in your project. The complete C# example project is available in the Aspose.HTML for .NET GitHub repository.

Convert SVG to PNG with One Line of C#

The static Converter class provides a compact way to render SVG content to PNG. The example below converts SVG to PNG with default image save options.

1// Convert SVG to PNG using C#
2
3// Invoke the ConvertSVG() method for SVG to PNG conversion
4Converter.ConvertSVG(Path.Combine(DataDir, "shapes.svg"), new ImageSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.png"));

Convert an SVG File to PNG in C#

Use this workflow when the SVG source exists on disk and the PNG should be saved to a file path.

The workflow is:

  1. Prepare the SVG source file or load it as an SVGDocument.
  2. Create an ImageSaveOptions instance. The default Format value is PNG.
  3. Call Converter.ConvertSVG(source, options, savePath).
  4. Save the generated PNG file to the target output path.
 1// Convert SVG to PNG in C#
 2
 3// Prepare SVG code
 4string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
 5              "<circle cx ='100' cy ='100' r ='60' fill='none' stroke='red' stroke-width='10' />" +
 6              "</svg>";
 7
 8// Prepare a path to save the converted file
 9string savePath = Path.Combine(OutputDir, "circle.png");
10
11// Create an instance of the ImageSaveOptions class
12ImageSaveOptions options = new ImageSaveOptions();
13
14// Convert SVG to PNG
15Converter.ConvertSVG(code, ".", options, savePath);

When to Use SVG to PNG Conversion

Use PNG output when the rendered SVG must keep crisp lines, transparent backgrounds, diagrams, icons, or UI details. PNG is often a better choice than JPG for technical graphics and documentation images. If smaller photographic previews are more important than lossless edges, use SVG to JPG. If the output must remain a document, use SVG to PDF.

Customize SVG to PNG with ImageSaveOptions

Use ImageSaveOptions when the generated PNG needs custom rendering settings.

To customize SVG to PNG conversion:

  1. Load the SVG source.
  2. Create ImageSaveOptions.
  3. Configure only the settings required by the output, such as PageSetup, BackgroundColor, resolution, or UseAntialiasing.
  4. Pass the SVG source, options, and output path to Converter.ConvertSVG().
OptionUse it to control
FormatThe output image format. The default value is PNG.
BackgroundColorThe color used to fill the image background. The default value is transparent.
PageSetupPage size, margins, and page layout settings.
HorizontalResolution and VerticalResolutionOutput resolution values used by the rendering process. The default value is 300 dpi for each property.
UseAntialiasingImage rendering quality. Antialiasing is enabled by default.
TextText rendering configuration through TextOptions.
 1// Convert SVG to PNG in C# with custom settings
 2
 3// Prepare a path to a source SVG file
 4string documentPath = Path.Combine(DataDir, "flower1.svg");
 5
 6// Prepare a path to save the converted file
 7string savePath = Path.Combine(OutputDir, "flower-options.png");
 8
 9// Initialize an SVG document from the file
10using SVGDocument document = new SVGDocument(documentPath);
11
12// Create an instance of the ImageSaveOptions class. Set up the SmoothingMode, resolutions, and change the background color to AliceBlue 
13ImageSaveOptions options = new ImageSaveOptions()
14{
15    HorizontalResolution = 200,
16    VerticalResolution = 200,
17    BackgroundColor = System.Drawing.Color.AliceBlue,
18    UseAntialiasing = true,
19};
20
21// Convert SVG to PNG
22Converter.ConvertSVG(document, options, savePath);

Related Image Workflows

Save SVG to PNG with an Output Stream Provider

Use ICreateStreamProvider when the conversion output should be written through streams instead of only to a file path. This is useful for memory-based workflows, remote storage, databases, and services that decide where rendered image output is stored.

 1// Implement a custom MemoryStream provider for advanced control over HTML rendering output streams
 2
 3class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
 4{
 5    // List of MemoryStream objects created during the document rendering
 6    public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
 7
 8    public Stream GetStream(string name, string extension)
 9    {
10        // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats
11        MemoryStream result = new MemoryStream();
12        Streams.Add(result);
13        return result;
14    }
15
16    public Stream GetStream(string name, string extension, int page)
17    {
18        // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
19        MemoryStream result = new MemoryStream();
20        Streams.Add(result);
21        return result;
22    }
23
24    public void ReleaseStream(Stream stream)
25    {
26        // Here you can release the stream filled with data and, for instance, flush it to the hard-drive
27    }
28
29    public void Dispose()
30    {
31        // Releasing resources
32        foreach (MemoryStream stream in Streams)
33            stream.Dispose();
34    }
35}

The following example converts SVG to PNG with MemoryStreamProvider.

 1// Convert SVG to PNG in C# using memory stream
 2
 3// Create an instance of MemoryStreamProvider
 4using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
 5
 6// Prepare SVG code
 7string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
 8              "<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />" +
 9              "</svg>";
10
11// Convert SVG to PNG using the MemoryStreamProvider
12Converter.ConvertSVG(code, ".", new ImageSaveOptions(), streamProvider);
13
14// Get access to the memory stream that contains the result data
15MemoryStream memory = streamProvider.Streams.First();
16memory.Seek(0, SeekOrigin.Begin);
17
18// Flush the result data to the output file
19using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.png")))
20{
21    memory.CopyTo(fs);
22}

Related SVG Conversion Guides

Other Platforms

Try Online SVG to PNG Conversion

                
            

Use the online SVG to PNG Converter for quick manual conversion. Use Aspose.HTML for .NET when SVG to PNG conversion must run inside a C# application, service, or document pipeline.

SVG to PNG Converter