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).
Asked
Active
Viewed 1,699 times
4
-
1Not sure how the question is too broad? I am asking a very specific question; no offence but the person who voted for it to be closed because it's too broad should reread the question. – Mayank Sep 01 '16 at 13:44
-
Maybe add how you would save them as GIF. Then asking for the svg one wouldn't too broad. – rene Sep 01 '16 at 19:34
1 Answers
6
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
});
}
}
}

Dax Pandhi
- 843
- 4
- 13

Mayank
- 8,777
- 4
- 35
- 60
-
Very useful! I suggest changing the first line in the `foreach` loop to the following: `var geometry = stroke.GetGeometry(stroke.DrawingAttributes).GetOutlinedPathGeometry();` This will ensure that if the strokes are created with different tools/attributes, they will be preserved. For example, if someone uses a square shaped nib instead of a round. EDIT: Edited the answer to reflect this. – Dax Pandhi May 02 '17 at 13:57
-
@DaxPandhi have you tried the edits? Just making sure that it works with your edits. If so then please confirm and I'll approve the edits. Thanks – Mayank May 03 '17 at 00:13
-
Yes, I have successfully used the modified code in my own application and it works well. I first tried the original code you had pasted, and while it worked, my different nib shapes were not stored. After modifying, I was able to preserve the shapes. – Dax Pandhi May 04 '17 at 08:38