SVG (Scalable Vector Graphics) is a vector image format for two-dimensional graphics. These images are based on XML format that enables you to traverse different nodes to edit or search the file contents easily. You can load, save or merge SVG image files programmatically in C#. Likewise, several other features related to SVG images allow your .NET based applications to process several supported file formats. Let us explore the following sections for further information:
- Install C# .NET SVG API
- Load SVG file Programmatically using C#
- Save SVG file Programmatically in C#
- Merge SVG images Programmatically with C#
Install C# .NET SVG API
Aspose.SVG for .NET API supports creating, editing, or converting SVG files. You can download the DLL file as ZIP from Official Downloads, and add it to the resources of your project. Moreover, you can also configure it via NuGet Package Manager in Microsoft Visual Studio. It will install the API from NuGet gallery along with other dependencies, if any. Below is the NuGet installation command:
PM> Install-Package Aspose.SVG
Load SVG file Programmatically using C#
While initiating work with existing SVG images, the first step we need to do is to load the input file. You can load an SVG file programmatically from a file as well as from the web. Below steps show how to load an SVG file:
- Specify the file path to input SVG
- OR specify the URL to load the SVG image
- Load an SVG document from a file or URL
The following code snippet shows how to load SVG file programmatically using C#:
However, it is important to note here that the API can throw DOMException if the resource is not found at specified URL.
Save SVG file Programmatically in C#
We have already learned how to load SVG file. Likewise, you can save an SVG file to a file or URL. The following steps explain how to save SVG files in your .NET based applications:
- Set a full (complete) path for SVG document saving
- Save SVG to a file
- Or Save SVG to Url
The Save(Url) overloaded method Saves the document to a local file specified by Url. The following code snippet shows how to save SVG file using C#:
Merge SVG images Programmatically with C#
You can merge different SVG files to PDF, XPS, JPG, TIFF, and other file formats. You need to follow the following steps to merge different SVG images:
- Load multiple SVG input files
- Initialize an instance of SvgRenderer
- Merge all SVG files to the required output format
The code below is an example of how to merge SVG files programmatically using C#:
Conclusion
In this article, we have explored how to load and save SVG files to a file or URL as per your requirements. Moreover, we have explored how to merge different SVG files to PDF, XPS, PNG, JPG, etc. However, the example and code snippet we have considered here is related to merging the SVG files to a PDF document. You can explore the API further by checking out the Example Project as well as the Product Documentation. In case of any query, you can always reach out to us at Free Support Forums. We look forward to getting in touch with you!