how to serialize StrokeCollection in c#
-
26-06-2021 - |
Question
i want to save a Stroke in a memorystream for this purpose is used BinaryFormatter but when i try to serialize Stroke i get a error that i can't serialize Stroke
is there any way to save a Stroke in a memorystream or serialize Stroke?
here is one part of my code
int size = inkCanvas1.Strokes.Count();
IFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
if (size != 0)
{
try
{
formatter.Serialize(stream, inkCanvas1.Strokes[size - 1]);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
thanks.
Solution
The reason that this doesn't work is that StrokeCollection
doesn't have the SerializableAttribute
applied.
But you can use the StrokeCollection.Save
method for this.
var ms = new MemoryStream();
using (ms)
{
StrokeCollection sc = ...;
sc.Save(ms);
ms.Position = 0;
}
And then when you need the StrokeCollection
again, you can use the constructor that accepts a Stream
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow