溫馨提示×

c# svg怎樣刪除

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

在C#中刪除SVG元素,你可以使用SvgDocument類的RemoveElement方法。以下是一個簡單的示例,演示了如何從SVG文檔中刪除一個元素:

using System;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        // 創(chuàng)建一個新的SVG文檔
        XDocument svgDoc = new XDocument(
            new XElement("svg",
                new XAttribute("width", "200"),
                new XAttribute("height", "200")));

        // 添加一個矩形元素
        XElement rect = new XElement("rect",
            new XAttribute("x", "50"),
            new XAttribute("y", "50"),
            new XAttribute("width", "100"),
            new XAttribute("height", "100"),
            new XAttribute("fill", "blue"));
        svgDoc.Root.Add(rect);

        // 刪除矩形元素
        svgDoc.Root.Remove(rect);

        // 保存SVG文檔
        svgDoc.Save("output.svg");
    }
}

在這個示例中,我們首先創(chuàng)建了一個新的SVG文檔,并向其中添加了一個矩形元素。然后,我們使用RemoveElement方法刪除了矩形元素,并將修改后的SVG文檔保存到文件中。

0