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, JPG, PNG, BMP, TIFF, and GIF.
You can download the complete examples and data files from GitHub.
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:
- Load an SVG document. You can load SVG from a file, SVG code, stream, or URL.
- Create a Save Options object. You may customize the rendering process to get the desired result.
- 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:
1using System.IO;
2using Aspose.Html.Dom.Svg;
3using Aspose.Html.Converters;
4using Aspose.Html.Saving;
5using System.Drawing;
6using System.Drawing.Drawing2D;
7...
8
9 // Prepare a path to a source SVG file
10 string documentPath = Path.Combine(DataDir, "flower1.svg");
11
12 // Prepare a path for converted file saving
13 string outputPath = Path.Combine(OutputDir, "flower-options.png");
14
15 // Initialize an SVG document from the file
16 using var document = new SVGDocument(documentPath);
17
18 // Initialize ImageSaveOptions. Set up the SmoothingMode and change the background color to AliceBlue
19 var options = new ImageSaveOptions()
20 {
21 BackgroundColor = System.Drawing.Color.AliceBlue,
22 SmoothingMode = SmoothingMode.HighQuality,
23 };
24
25 // Convert SVG to PNG
26 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.
Aspose.HTML offers free online SVG Converters 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, or SVG to GIF. Just select the file, choose the format to convert, and you’re done. It’s fast and completely free!