温馨提示×

c# svg怎样写入

c#
小樊
81
2024-10-18 01:51:11
栏目: 编程语言

在C#中,你可以使用System.Xml.Linq命名空间中的XDocumentXElement类来操作和生成SVG文件。以下是一个简单的示例,演示了如何创建一个包含基本形状(矩形和圆形)的SVG文件:

using System;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        // 创建一个新的SVG文档
        XDocument svgDoc = new XDocument(
            new XElement("svg",
                new XAttribute("width", "800"),
                new XAttribute("height", "600"),
                new XElement("rect",
                    new XAttribute("x", "50"),
                    new XAttribute("y", "50"),
                    new XAttribute("width", "200"),
                    new XAttribute("height", "100"),
                    new XAttribute("fill", "blue")
                ),
                new XElement("circle",
                    new XAttribute("cx", "400"),
                    new XAttribute("cy", "300"),
                    new XAttribute("r", "50"),
                    new XAttribute("fill", "red")
                )
            )
        );

        // 将SVG文档保存到文件
        svgDoc.Save("output.svg");
    }
}

这个示例创建了一个包含一个矩形和一个圆形的简单SVG文件。你可以根据需要修改这个示例,以创建更复杂的SVG图形。注意,这个示例使用了XDocument类,它提供了更多的功能和灵活性,相对于XElement类。

0