Search code examples
c#wpfserializationinkcanvas

Save and load Ink Canvas StrokeCollection to/from stream


I'm trying to serialize an ink canvas in WPF. So I'm using StrokeCollection.Save to save the strokes to a MemoryStream. But when I try to load the stream into the InkCanvas I get an ArgumentException stating "The length of the ISF data must be greater than zero."
Here's the code sample:

using (MemoryStream ms = new MemoryStream())
{
    inkcanvas.Strokes.Save(ms);
    inkcanvas.Strokes = new System.Windows.Ink.StrokeCollection(ms);
}

What am I missing here?


Solution

  • Did you check the Position of the your stream?

    I think that after saving the stroke into it it will point to the end of it.
    Try to reset the position to the first character, like this:

    using (MemoryStream ms = new MemoryStream())
    {
        inkcanvas.Strokes.Save(ms);
        ms.Position = 0;
        inkcanvas.Strokes = new System.Windows.Ink.StrokeCollection(ms);
    }