c# svg怎樣寫入

c#
小樊
81
2024-10-18 01:51:11
欄目: 編程語言

在C#中,你可以使用System.Xml.Linq命名空間中的XDocumentXElement類來操作和生成SVG文件。以下是一個(gè)簡(jiǎn)單的示例,演示了如何創(chuàng)建一個(gè)包含基本形狀(矩形和圓形)的SVG文件:

using System;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        // 創(chuàng)建一個(gè)新的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");
    }
}

這個(gè)示例創(chuàng)建了一個(gè)包含一個(gè)矩形和一個(gè)圓形的簡(jiǎn)單SVG文件。你可以根據(jù)需要修改這個(gè)示例,以創(chuàng)建更復(fù)雜的SVG圖形。注意,這個(gè)示例使用了XDocument類,它提供了更多的功能和靈活性,相對(duì)于XElement類。

0