问题
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:
I figured it out.
Here are the steps
- 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).
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
});
}
}
}
来源:https://stackoverflow.com/questions/39261548/wpf-inkcanvas-save-stokes-as-svg