uwp InkCanvas将stokes保存为svg

时间:2017-07-14 16:07:59

标签: c# svg uwp

我正在尝试将InkCanvas,InkStorkes保存为SVG。我找到了一个适用于Wpf的上一个问题,但是我无法让它与uwp一起工作。 Wpf InkCanvas save stokes as svg 。我对它进行了一些更改,但是我遇到了GetGrometry和XamlWriter的问题。

 var svg = new SvgDocument();
        var colorServer = new SvgColourServer(System.Drawing.Color.Black);

        var group = new SvgGroup { Fill = colorServer, Stroke = colorServer };
        svg.Children.Add(group);

        foreach (var stroke in inkCanvas.InkPresenter.StrokeContainer.GetStrokes())
        {

            var geometry = stroke.GetGeometry(stroke.DrawingAttributes).GetOutlinedPath‌​Geometry();

            var s = XamlWriter.Save(geometry);

            if (s.IsNotNullOrEmpty())
            {
                var element = XElement.Parse(s);

                var data = element.Attribute("Figures")?.Value;

                if (data.IsNotNullOrEmpty())
                {
                    group.Children.Add(new SvgPath
                    {
                        PathData = SvgPathBuilder.Parse(data),
                        Fill = colorServer,
                        Stroke = colorServer
                    });
                }
            }
        }

1 个答案:

答案 0 :(得分:-1)

我从Xavier Xie-MSFT评论中实施了一种方法。

主要思想是使用自己的ICanvasPathReceiver接口实现-这是CanvasGeometryToSvgPathReader类:

var svgDocument = new CanvasSvgDocument(canvasDevice);

foreach (var stroke in InkCanvas.InkPresenter.StrokeContainer.GetStrokes())
{
    var canvasGeometry = CanvasGeometry.CreateInk(drawingSession, new[] {stroke});

    var pathReceiver = new CanvasGeometryToSvgPathReader();
    canvasGeometry.SendPathTo(pathReceiver);

    var element = svgDocument.Root.CreateAndAppendNamedChildElement("path");

    element.SetStringAttribute("d", pathReceiver.Path);
    element.SetColorAttribute("fill", stroke.DrawingAttributes.Color);
}

结果是,圆珠笔呈现的很好,荧光笔的表现更差,铅笔没有呈现。

完整的源代码:https://github.com/ycherkes/InkToSvg