【问题标题】:Error with adding "rect" element to document by c# program通过 c# 程序将“rect”元素添加到文档时出错
【发布时间】:2019-05-14 12:24:01
【问题描述】:

我正在尝试通过 c# 控制台应用程序向 svg 文档添加一些“rect”元素。 正在添加一个“rect”元素,它们存在于 svg 文档的结构中,但它们是不可见的。

当我手动添加“rect”元素时,一切正常。

应用执行后的 Svg 图像

手动添加“rect”后的Svg

这是我的代码

    public static MemoryStream Draw(Stream stream)

    {
        var outputStream = new MemoryStream();

        var svgDocument = XDocument.Load(stream);

        if (svgDocument.Root != null)
        {
            var gElements = svgDocument.Root.Elements("{http://www.w3.org/2000/svg}g");

            var damageLayer = gElements.FirstOrDefault(x => x.Attribute("id")?.Value == "Damages");

            var damage = new XElement("rect", new XAttribute("x", 205), new XAttribute("y", 205), new XAttribute("width", 15), new XAttribute("height", 15));

            damageLayer.Add(damage);
        }

        svgDocument.Save(outputStream);

        return outputStream;
    }

您有任何解决该问题的建议或方法吗?如果你这样做,请告诉我。任何帮助表示赞赏

【问题讨论】:

标签: c# svg linq-to-xml


【解决方案1】:

您需要在 SVG 命名空间中创建矩形,即

XNamespace namespace = "http://www.w3.org/2000/svg";
var damage = new XElement(namespace + "rect", ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多