SVG Converter | C#

SVG files are Scalable Vector Graphics Files that use XML based text format for describing the appearance of an image. The word Scalable refers to the fact that SVG can be scaled to different sizes without losing any quality. A text-based description of such files makes them independent of resolution. It is one of the most used formats for website building and print graphics to achieve scalability.

The main highlight of Aspose.HTML is the conversion feature. Converting between formats is required for various reasons: to work in a familiar, convenient format or to take advantage of different formats for specific tasks. The Aspose.Html.Converters namespace implements easy access to conversion methods. It provides a wide range of SVG conversions to popular formats, such as PDF, XPS, DOCX, JPEG, PNG, BMP, TIFF, and GIF.

The articles in this section provide information on the list of supported SVG conversions and how to perform them using ConvertSVG() methods. It can be different scenarios, but any SVG conversion can be made with a few required steps:

  1. Load an SVG document. You can load SVG from a file, SVG code, stream, or URL.
  2. Create a Save Options object. You may customize the rendering process to get the desired result.
  3. Invoke one of the ConvertSVG() methods and pass the required parameters to it.

Please take a look over the following C# code snippet, which shows the process of converting SVG to PNG with ImageSaveOptions specifying:

using System.IO;
using Aspose.Html.Dom.Svg;
using Aspose.Html.Converters;
using Aspose.Html.Saving;
using System.Drawing;
using System.Drawing.Drawing2D;
...  

    // Prepare a path to a source SVG file
    string documentPath = Path.Combine(DataDir, "flower1.svg");
    
    // Prepare a path for converted file saving 
    string outputPath = Path.Combine(OutputDir, "flower-options.png");
    
    // Initialize an SVG document from the file
    using var document = new SVGDocument(documentPath);
    
    // Initialize ImageSaveOptions. Set up the SmoothingMode and change the background color to AliceBlue 
    var options = new ImageSaveOptions()
    {
        BackgroundColor = Color.AliceBlue,
        SmoothingMode = SmoothingMode.HighQuality,
    };            
    
    // Convert SVG to PNG 
    Converter.ConvertSVG(document, options, outputPath);

In the example, the SVGDocument(address) constructor initializes an SVG document from the file. The ImageSaveOptions() constructor creates a new ImageSaveOptions object with PNG ImageFormat, SmoothingMode and BackgroundColor properties. By default, the Format property is PNG. Then, the ConvertSVG() method takes SVGDocument, ImageSaveOptions, and output path and performs SVG to PNG conversion.

Online SVG Converter

You can convert SVG to other formats with Aspose.HTML API in real time. First, load an SVG file from your local drive and then run the example. The save options in the example are set by default. You will immediately receive the conversion 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