我正在尝试将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
                    });
                }
            }
        }

最佳答案

我从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

关于c# - uwp InkCanvas将笔画另存为svg,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45107362/

10-11 02:05