How to Convert SVG Files

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, JPEG, 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.

Code example in C# to convert document formats
Input file
Upload a file you want to convert
Output format
Select the target format

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. For more information, please see the article How to Merge SVG Documents .

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

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:

using Aspose.Svg;
using System.IO;
using Aspose.Svg.Saving;
using Aspose.Svg.Converters;
...
    
    // Initialize an SVG document from a file
    using (var document = new SVGDocument(Path.Combine(DataDir, "svg-to-png.svg")))
    {
        // Create an instance of the ImageSaveOptions class
    	var pngSaveOptions = new ImageSaveOptions();    

        // Convert SVG to PNG
    	Converter.ConvertSVG(document, pngSaveOptions, Path.Combine(OutputDir, "svg-to-png.png"));
    }

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.

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:

using Aspose.Svg;
using System.IO;
using Aspose.Svg.Rendering.Image;
...
	
	// Open a source SVG document 
	using (var document = new SVGDocument(Path.Combine(DataDir, "light.svg")))
	{
	    // Initialize an instance of the ImageRenderingOptions class 
		var pngOptions = new ImageRenderingOptions();
	    	    

		// Initialize an instance of ImageDevice and specify the output file to render
	    using (var device = new ImageDevice(pngOptions, Path.Combine(OutputDir, "light_out.png")))
	    {
	        // Render SVG to PNG
			document.RenderTo(device);
		}
	}

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.

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.

Text “Banner SVG Converter”