In WPF I need to binary serialize a Polyline variable with a large number of points. I use the following code:
SaveFileDialog sf = new SaveFileDialog();
sf.FileName = "Orig.txt");
if (sf.ShowDialog() == true)
{
FileStream fs = new FileStream(sf.FileName, FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, plOriginal);
}
catch (Exception exc)
{
MessageBox.Show("Failed to serialize. Reason: " + exc.Message);
}
fs.Close();
}
but I get the error:
System.Runtime.Serialization.SerializationException: Type 'System.Windows.Media.PolyLineSegment' in Assembly 'PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable. at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph) at CurveAnalyzerDemoWPF.MainWindow.rbSave_Click(Object sender, RoutedEventArgs e) in MainWindow.xaml.cs:line 92}
So any workaround to make it work? thanx in advance
EDIT: I put the serializable attribute on the polyline variable:
[Serializable]
private PolyLineSegment plOriginal = new PolyLineSegment();
but I got the compilation following error: Error 1 Attribute 'Serializable' is not valid on this declaration type. It is only valid on 'class, struct, enum, delegate' declarations. F:\C# WPF\CurveAnalyzerDemoWPF\CurveAnalyzerDemoWPF\MainWindow.xaml.cs 55 4 CurveAnalyzerDemoWPF