How to Convert SVG Files – C# Examples

Converting SVG documents to other formats is one of the main features of Aspose.SVG for .NET API. Converting is required for various reasons: to work in a familiar, convenient format or to take advantage of different formats for specific tasks. The articles in this section provide information on a list of supported SVG conversion scenarios and how to execute them in C#.

Aspose.SVG for .NET API can convert SVG files to PDF, XPS, JPG, PNG, BMP, TIFF, and GIF file formats. You can use API in your C# or any other .NET application (such as VB.NET) to develop converter applications without getting into the details of underlying file formats.

Online SVG Converter

You can convert SVG to other formats with Aspose.SVG API in real time. Please load SVG from the local file system, select the output format and run the example. In this example, the save options are set by default. You will immediately receive the result as a separate file.

                
            

A Few Ways to Convert SVG in C#

You can convert SVG to various popular formats in any way – online or programmatically. Converting from SVG to other formats can perform by using ConvertSVG() methods of the Converter class, the RenderTo(device) method of the SVGDocument class, or the Render(IDevice, TDocument) method of the Renderer class.

The current section describes supported scenarios of SVG files conversions to other popular formats by using Converter and SVGDocument classes:

  1. The static Converter class is a shared facade that provides SVG files conversions to the popular formats and allows to make these operations convenient and easy. A wide range of ConvertSVG() methods take as arguments the source document, save options, output file path, and convert SVG to PDF, XPS or Image files.
  2. The RenderTo() method of the SVGDocument class is used to render SVG to another format and send a document to the output device. Aspose.SVG API provides the following output devices implementation: the PdfDevice, XpsDevice and ImageDevice classes, which perform rendering to PDF, XPS and Image file formats respectively.
  3. The Render(IDevice, TDocument) method of the Renderer class gives you the ability to send multiple documents at once to the output rendering device and merge them.

Let’s consider both scenarios of conversion SVG document to another file format, for example, SVG to PNG:

You can download the complete examples and data files from GitHub. About downloading from GitHub and running examples, you find out from the How to Run the Examples section.

Convert SVG to PNG Using the ConvertSVG() Method

Converting a file to another format using the ConvertSVG() method is a sequence of operations among which document loading and saving:

 1using Aspose.Svg;
 2using System.IO;
 3using Aspose.Svg.Saving;
 4using Aspose.Svg.Converters;
 5...
 6
 7    // Initialize an SVG document from a file
 8    using (var document = new SVGDocument(Path.Combine(DataDir, "svg-to-png.svg")))
 9    {
10        // Create an instance of the ImageSaveOptions class
11    	var pngSaveOptions = new ImageSaveOptions();
12
13        // Convert SVG to PNG
14    	Converter.ConvertSVG(document, pngSaveOptions, Path.Combine(OutputDir, "svg-to-png.png"));
15    }

In the example, the ImageSaveOptions() constructor initializes an instance of the ImageSaveOptions class that is passed to ConvertSVG() method. The ConvertSVG(source, options, outputPath) method takes the required attributes and performs the conversion operation.

The figure illustrates the svg-to-png.png file.

Text “svg-to-png.svg file rendered to PNG”

Convert SVG to PNG Using the RenderTo() Method

To convert SVG to PNG using the RenderTo() method, take the following stages:

 1using Aspose.Svg;
 2using System.IO;
 3using Aspose.Svg.Rendering.Image;
 4...
 5
 6	// Open a source SVG document 
 7	using (var document = new SVGDocument(Path.Combine(DataDir, "light.svg")))
 8	{
 9	    // Initialize an instance of the ImageRenderingOptions class 
10		var pngOptions = new ImageRenderingOptions();
11
12		// Initialize an instance of ImageDevice and specify the output file to render
13	    using (var device = new ImageDevice(pngOptions, Path.Combine(OutputDir, "light_out.png")))
14	    {
15	        // Render SVG to PNG
16			document.RenderTo(device);
17		}
18	}

The ImageDevice(options, file) constructor takes as arguments an instance of ImageRenderingOptions class, output file name and initializes a new instance of the ImageDevice class. The RenderTo(device) method converts and sends the current document to the output rendering device.

The figure illustrates the result of SVG to PNG conversion – light.png file.

Text “light.svg file rendered to PNG using RenderTo() method”

General Options

You can customize the rendering process by specifying the page size, margins, background color, etc. For converting SVG to all mentioned above formats, from the RenderingOptions class the following properties are inherited: BackgroundColor, Css, HorizontalResolution, PageSetup, and VerticalResolution.

Every output device PdfDevice, XpsDevice and ImageDevice has its own unique set of options implemented with classes PdfRenderingOptions, XpsRenderingOptions and ImageRenderingOptions respectively.

The options that are implementing with the PdfSaveOptions, XpsSaveOptions and ImageSaveOptions classes are inheriting from the PdfRenderingOptions, XpsRenderingOptions and ImageRenderingOptions classes respectively.

Aspose.SVG offers a free online SVG Converter for converting SVG files to a variety of popular formats. You can easily convert SVG to PDF, SVG to XPS, SVG to JPG, SVG to PNG, SVG to BMP, SVG to TIFF, SVG to GIF. Just select a file, choose the format to convert, and you’re done. It’s fast and completely free!

Text “Banner SVG Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.