用 C# 编辑 SVG

学习如何使用 C# 以编程方式编辑 SVG

如何用 C# 编辑 SVG

Aspose.SVG for .NET 库提供了一组类和方法,用于创建、编辑、转换 SVG 文件并进行其他操作。创建文档对象后,就可以在其中填充 SVG 元素。您可以通过添加 SVG 图形、文本、过滤器等更多元素来定制文档。在本文中,我们将探讨两个 C# 示例,它们展示了构建和编辑 SVG 的不同方法:一个使用 Aspose.SVG Builder API ,另一个使用直接设置元素和属性的手动 DOM 操作。


首先,确保您的项目中安装了 Aspose.SVG for .NET API。安装过程非常简单。您可以使用以下命令通过 NuGet 包管理器控制台进行安装:


安装 Aspose.SVG for .NET

Install-Package Aspose.SVG



使用元素构建器向 SVG 文件添加元素

元素生成器是 Aspose.SVG 生成器 API 的专用类,可让您更轻松地在代码中创建和修改 SVG 元素。它们通常使用流畅构建器模式(Fluent Builder Pattern)等模式,为以编程方式设置属性和构建 SVG 内容提供简洁、可读的语法。下面的代码演示了使用 C# 从零开始制作 SVG 的简洁而优雅的方法。该示例利用 SVGSVGElementBuilder 中的 Fluent Builder 模式来构建 SVG 并添加新元素:装饰圆和文本。


使用 Element Builders 编辑 SVG 的 C# 代码

using Aspose.Svg.Builder;
using System.Drawing;
using System.IO;
...
    using (var document = new SVGDocument())
    {
        // Create an <svg> element with specified width, height and viewBox, and add into it other required elements
        var svg = new SVGSVGElementBuilder()
            .Width(700).Height(300)
            .ViewBox(0, 0, 700, 300)

            .AddG(g => g
            .AddCircle(circle => circle.Cx(130).Cy(130).R(60).Fill(Paint.None).Stroke(Color.Salmon).StrokeWidth(70).StrokeDashArray(2,14))
            .AddText("I can add elements to SVG!", x: 270, y: 140, fontSize: 30, fill: Color.DarkRed)
            )
            .Build(document.FirstChild as SVGSVGElement);
        
        // Save the document
        document.Save(Path.Combine(OutputDir, "svg-from-scratch.svg"));
    }



向 SVG 文件添加元素

文档对象模型(DOM)API 提供了对 SVG 节点及其属性的完全控制,并且完全符合官方 SVG 规范。您可以通过添加新节点、删除现有节点或编辑其内容来修改文档。例如,下面是如何在 SVG 中添加装饰圆和文本:


使用底层 DOM 操作编辑 SVG 的 C# 代码

using System.IO;
using Aspose.Html;
...

    // Set SVG Namespace Url
    string SvgNamespace = "http://www.w3.org/2000/svg";

    using (var document = new SVGDocument())
    {
        var svgElement = document.RootElement;

        // Create a text element and set its attributes
        var textElement = (SVGTextElement)document.CreateElementNS(SvgNamespace, "text");
        textElement.Style.FontSize = "2.0em";
        textElement.SetAttribute("x", "270px");
        textElement.SetAttribute("fill", "darkred");
        textElement.SetAttribute("y", "140px");
        textElement.TextContent = "I can add elements to SVG!";

        // Create a circle element and set its attributes
        var circleElement = (SVGCircleElement)document.CreateElementNS(SvgNamespace, "circle");
        circleElement.Cx.BaseVal.Value = 130;
        circleElement.Cy.BaseVal.Value = 130;
        circleElement.R.BaseVal.Value = 60;
        circleElement.SetAttribute("stroke", "salmon");
        circleElement.SetAttribute("stroke-width", "70");
        circleElement.SetAttribute("fill", "none");
        circleElement.SetAttribute("stroke-dasharray", "2,14");

        // Add the text and circle elements into svgElement
        svgElement.AppendChild(textElement);
        svgElement.AppendChild(circleElement);

        // Save the document
        document.Save(Path.Combine(OutputDir, "svg-from-scratch.svg"));
    }





让我们看一下编辑 SVG 文档的简单步骤,以说明上面的 С# 代码。 SVG <circle><text> 元素将添加到创建的 SVG 文档中:

  1. 要以编程方式从头开始创建 SVG 文档,请使用不带参数的 SVGDocument() 构造函数。
  2. SVGDocument 类的RootElement属性指向文档的根<svg>元素。
  3. 创建带有属性的 <text><circle> 元素,并将它们添加到 <svg> 元素:
    • 您可以使用 CreateElementNS() 方法创建 SVGTextElement 和 SVGCircleElement 类的实例。
    • 调用 SetAttribute() 方法设置指定位置、大小、填充等的属性。
    • 使用 AppendChild() 方法将元素添加到 <svg> 中。
  4. 使用 Save() 方法保存编辑后的SVG文件。

我们在上面创建的文件在两个代码示例中都是一样的(svg-from-scratch.svg),看起来像这样:

文本 “带有圆形和文本的 SVG 图像


有用的资源

  • 编辑 SVG 文件 – C# 示例 文档文章为您提供了如何使用 Aspose.SVG for .NET API 阅读或编辑文档对象模型的基本信息。您将了解如何创建 SVG 元素以及如何使用这些元素–通过插入新节点、删除或编辑现有节点的内容来修改文档。
  • SVG Builder – 高级 SVG 创建和修改 一章中,您将找到使用 Aspose.SVG Builder API 有效操作 SVG 的指南,涵盖从创建基本元素到高级技术(如混合体和语法糖)等各个方面。
  • 元素生成器 – Element Builders 一文深入介绍了 Aspose.SVG Builder API 中使用的元素构建器。您将了解 SVGElementBuilder 类、其专用构建器以及它们如何简化 SVG 编程。


  • 其他支持的功能

    使用 Aspose.SVG C# 库转换、合并、编辑 SVG 文档、转换颜色代码、矢量化图像等!