WPF InkCanvas将笔画另存为SVG [英] Wpf InkCanvas save stokes as svg
本文介绍了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).
推荐答案
我知道了.
这是步骤
- 遍历
StrokeCollection
- 通过调用
GetGeometry
函数,然后调用GetOutlinedPathGeometry
,获取每个Stroke
的PathGeometry
. - 从
Geometry
中取出Figures
.我通过将PathGeometry
保存到XAML,然后通过XElement.Parse
解析Figures
属性来做到这一点. - 然后我可以创建一个svg文档并添加每个路径(请参见下面的代码).
- Iterate over the
StrokeCollection
- Get
PathGeometry
of eachStroke
by callingGetGeometry
function and then callingGetOutlinedPathGeometry
. - Get
Figures
out ofGeometry
. I am doing it by saving thePathGeometry
to XAML and then parsing theFigures
attribute byXElement.Parse
. - 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).GetOutlinedPathGeometry();
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屋!
查看全文