Save SVG in C#

Learn how to save SVG programmatically using Aspose.SVG for .NET.

How to Save SVG in C#

Aspose.SVG for .NET library provides overloaded Save() methods that allow saving SVG documents to local files, streams, Zip archives, or URLs supporting both basic and advanced scenarios. You can save documents directly using either a file path or a Url object. Optional parameters are available to specify the output format (such as SVG, SVGZ, etc.) and SVGSaveOptions for more detailed control over the saving process. Furthermore, the Aspose.SVG for .NET API supports saving through ResourceHandler objects, which enable customized resource storage mechanisms like memory streams or a Zip archive.


Let’s walk through how to save an SVG document programmatically. But first, ensure that you have Aspose.SVG for .NET installed in your project. Installing the API is easy – use the NuGet Package Manager Console and run the following command:


Install Aspose.SVG for .NET

Install-Package Aspose.SVG



Save SVG to a File

Here’s a simple way to save an SVG document to a file. This example loads an existing SVG file, allows you to modify it if you want, and then saves the updated version to a new one:

  1. Load an SVG document using the SVGDocument() constructor. Work with the document.
  2. Save the SVG file using the Save(path) method.

C# code to save SVG document

using Aspose.Svg;
...

    // Load the SVG document from a file
    using (SVGDocument document = new SVGDocument("document.svg"))
    {
        // Work with the document

        // Save SVG to the file
        document.Save("document-edited.svg");
    }



Convert and Save SVG as SVGZ

SVGZ files are standard SVG documents compressed with GZIP. They retain the full fidelity and scalability of the original SVG while significantly reducing file size – often by 60–80%. The following C# snippet loads an SVG file and saves it to a Url in compressed SVGZ format:


C# code to save SVG as SVGZ

using System.IO;
using Aspose.Svg;
...

    // Set a full path to save an SVG document
    Url url = new Url(Path.Combine(OutputUrl, "shapes_out.svgz"), Directory.GetCurrentDirectory());

    //  Load the SVG document from a file
    using (SVGDocument document = new SVGDocument(Path.Combine(DataDir, "shapes.svg")))
    {
        // Work with the document

        // Save the document as SVGZ to a Url
        document.Save(url, SVGSaveFormat.SVGZ);
    }





  1. The Url object specifies the target path for saving the compressed file. It combines the output directory and filename.
  2. SVGDocument loads the original SVG file into memory, allowing you to parse or edit its structure.
  3. The Save(url, saveFormat) method exports the document in SVGZ format by specifying SVGSaveFormat.SVGZ. The output will be a GZIP-compressed version of the original SVG.

Useful Resources



Other Supported Features

Use the Aspose.SVG for .NET library to convert, merge, edit SVG documents, convert color codes, vectorize images and more!