WPF InkCanvas将笔画另存为SVG [英] Wpf InkCanvas save stokes as svg

查看:355
本文介绍了WPF InkCanvas将笔画另存为SVG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将InkCanvas笔划集合保存到svg图像?我唯一能找到的就是可以将笔画保存为带有嵌入式ISF(墨水序列化格式)的GIF或将其渲染为位图.我想将笔触保存为可以与其他平台(例如网络)互操作的矢量格式.

Is it possible to save InkCanvas stroke collection to svg image? Only thing I can find is that I can save the strokes as GIF with embedded ISF (Ink Serialized Format) or maybe render them as bitmap. I want to save strokes in vector format that can be interoperable with other platforms (like web).

推荐答案

我知道了.

这是步骤

  1. 遍历StrokeCollection
  2. 通过调用GetGeometry函数,然后调用GetOutlinedPathGeometry,获取每个StrokePathGeometry.
  3. Geometry中取出Figures.我通过将PathGeometry保存到XAML,然后通过XElement.Parse解析Figures属性来做到这一点.
  4. 然后我可以创建一个svg文档并添加每个路径(请参见下面的代码).
  1. Iterate over the StrokeCollection
  2. Get PathGeometry of each Stroke by calling GetGeometry function and then calling GetOutlinedPathGeometry.
  3. Get Figures out of Geometry. I am doing it by saving the PathGeometry to XAML and then parsing the Figures attribute by XElement.Parse.
  4. Then I can just create a svg document and add each path (see code below).

我正在使用 SVG渲染库来创建SVG文档.

I am using SVG Rendering Library to create SVG document.

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.Strokes)
  {
      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
               });
           }
       }
}

这篇关于WPF InkCanvas将笔画另存为SVG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆