Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
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"));Use this workflow when the SVG source exists on disk and the PNG should be saved to a file path.
The workflow is:
SVGDocument.ImageSaveOptions instance. The default Format value is PNG.Converter.ConvertSVG(source, options, savePath). 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);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.
Use ImageSaveOptions when the generated PNG needs custom rendering settings.
To customize SVG to PNG conversion:
ImageSaveOptions.PageSetup, BackgroundColor, resolution, or UseAntialiasing.Converter.ConvertSVG().| Option | Use it to control |
|---|---|
Format | The output image format. The default value is PNG. |
BackgroundColor | The color used to fill the image background. The default value is transparent. |
PageSetup | Page size, margins, and page layout settings. |
HorizontalResolution and VerticalResolution | Output resolution values used by the rendering process. The default value is 300 dpi for each property. |
UseAntialiasing | Image rendering quality. Antialiasing is enabled by default. |
Text | Text 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);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}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.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.