SVG.NET Documentation

repository·master·Indexed 23 days ago

https://github.com/svg-net/svg

A C# library for reading, writing, and rendering SVG 1.1 images. Compatible with .NET Standard 2.0 and cross-platform (Windows, Linux, macOS), it is a community-maintained fork of the original Microsoft SVG.NET library. The library includes the SvgConsole CLI utility for converting SVG files to PNG images and provides tools for rendering SvgDocuments to bitmaps, handling custom attributes, and managing GDI+ dependencies on non-Windows platforms.

Tokens
5.1K
Snippets
18
Records
43
Agent score
75%

What's inside SVG.NET

  1. Overview of SVG.NET

    master

    SVG.NET is a C# library designed for reading, writing, and rendering SVG 1.1 images. It is suitable for applications built on the .NET framework and is compatible with any platform implementing .NET Standard 2.0.

    Platform Support:

    • Windows: Full support.
    • Linux: Supported.
    • macOS: Supported, though there are some limitations regarding rendering capabilities.
  2. Understand the Microsoft Public License (Ms-PL) for SVG.NET

    master

    SVG.NET is licensed under the Microsoft Public License (Ms-PL). By using the software, you accept the terms of this license.

    Key Rights

    • Copyright Grant: You are granted a non-exclusive, worldwide, royalty-free copyright license to reproduce the software, prepare derivative works, and distribute the software or your derivative works.
    • Patent Grant: You are granted a non-exclusive, worldwide, royalty-free license under any licensed patents to make, use, sell, or otherwise dispose of the software or its derivative works.

    Key Conditions and Limitations

    • No Trademark License: You do not have rights to use any contributor's name, logo, or trademarks.
    • Patent Retaliation: If you bring a patent claim against any contributor regarding patents infringed by the software, your patent license from that contributor terminates automatically.
    • Notice Retention: If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices.
    • Distribution Requirements:
      • Source Code: If distributing in source code form, you must include a complete copy of the Ms-PL license.
      • Compiled/Object Code: If distributing in compiled form, you may only do so under a license that complies with the Ms-PL.
    • Disclaimer of Warranty: The software is provided "as-is" without express warranties or guarantees. You bear the risk of using the software.
  3. Understand the Svg namespace

    master
    The Svg namespace is the primary entry point for the library. It contains the core classes and interfaces that implement the SVG specifications. While these classes are modeled after the W3C SVG interfaces, they are not strict implementations of them, but rather a functional C# representation designed for programmatic SVG manipulation.
  4. New SVG features supported in SVG.NET

    master

    The library has added support for several advanced SVG features:

    • Switch Element: Support for the Switch element with the systemLanguage selector attribute (Version 3.4.8).
    • CSS Variables: Support for SvgCssVariablePaintServer, allowing declared CSS variables to be used as paint servers (Version 3.4.8).
    • SVG2 Features: Support for miter-clip and arcs line joins, and the fr property for radial gradients (Version 3.2.3).
    • Path Commands: Support for relative commands in Paths (Version 3.4.0).
  5. Server-side usage and thread safety considerations

    master

    While SVG.NET can be used in server-side environments (like ASP.NET MVC or APIs), there are important considerations:

    1. System.Drawing Availability: Some environments (like certain Azure services) may not provide System.Drawing because it relies on GDI calls.
    2. Resource Intensity: Rendering large or complex SVGs to high-resolution bitmaps is resource-heavy. Running many such processes in parallel can impact server performance and availability.
    3. Error Handling: System.Drawing may not always fail gracefully under heavy load, sometimes resulting in cryptic errors that are difficult to debug.
    4. Concurrency: While some parallel work is possible, high levels of concurrency can lead to resource exhaustion.
  6. Choose the right SVG.NET version

    master

    SVG.NET has two main supported version tracks:

    • Version 3.x (Recommended): The latest version providing maximum flexibility and portability. It supports .NET Core (Windows, Linux, and macOS) and maintains compatibility with .NET Framework (3.5 and above).
    • Version 2.4. aked*: A .NET Framework-specific version. It is considered stable for legacy .NET projects but lacks .NET Core compatibility.

    If you are starting a new project, use the latest 3.x version. If upgrading from 2.4 to 3.x, validate your code as behavior may differ slightly between versions.

  7. Security: Mitigated XXE and untrusted file vulnerabilities

    master

    As of version 3.3.0, the library includes security enhancements for handling untrusted SVG files:

    • XXE Mitigation: The default behavior of DTD resolution has changed so that external references are not resolved by default.
    • Resource Safety: The default behavior for external references to images, text definitions, and other resources is changed to prevent automatic resolution, improving the safety of rendering untrusted files.
  8. Compatibility and Runtime requirements for SVG.NET

    master

    Depending on the version you are using, the supported runtimes vary significantly:

    • Version 3.0.102 and later: Removed support for .NET 3.5.
    • Version 3.0.84: This is the last release that supports .NET 3.5. Note that .NET Standard support in this version is preliminary and incomplete.
    • Version 3.0.49: The first version to support .NET Core alongside .NET. Building from source requires at least Visual Studio 2017.
    • Version 3.0.49 (Non-Windows): On macOS or Linux, the library will throw a descriptive exception if the GDI+ library is missing.
  9. Modify SVG elements and save to PNG

    master
    The SvgRuntimeUpdates sample demonstrates how to programmatically access and alter elements within the SVG source tree. This is useful for dynamic styling or structural changes before exporting the result to a bitmap format like PNG.
  10. Link the SVG.NET project to your application

    master

    If you have built the library from source and want to link it to your application via the .NET CLI, use the add reference command. This approach automatically inherits all necessary references, including Mac/Linux compatibility packages.

    dotnet add reference SVG/sources/Svg.csproj

    (Note: Replace SVG/sources/Svg.csproj with the actual path to the Svg project on your machine.)

    Alternatively, you can manually reference the compiled Svg.dll file from the output folder, but you must manually manage additional tooling/packages for Mac and Linux.

    dotnet add reference SVG/sources/Svg.csproj