本文介绍了uwp InkCanvas 将斯托克斯保存为 svg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 InkCanvas 、 InkStorkes 保存为 SVG.我发现了一个适用于 Wpf 的上一个问题,但我一直无法让它与 uwp 一起使用.Wpf InkCanvas 将斯托克斯保存为 svg .我对其进行了一些更改,但遇到了 GetGrometry 和 XamlWriter 的问题.

I am trying to save InkCanvas , InkStorkes as a SVG. I found a previous question that works for Wpf,but I have been unable to get it to work with uwp. Wpf InkCanvas save stokes as svg . I made some changes to it, but I am running into issues with GetGrometry and 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 的评论中实现了一种方法.

I implemented an approach from the comments Xavier Xie - MSFT.

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

The main idea was to use own implementation of ICanvasPathReceiver interface - here is CanvasGeometryToSvgPathReader class:

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);
}

结果圆珠笔渲染得很好,荧光笔更差,铅笔不渲染.

As result ballpoint pen renders fine, highlighter worse and pencil doesn't render.

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

这篇关于uwp InkCanvas 将斯托克斯保存为 svg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 01:17