Reputation: 1755
I have a Shape class wich contains
public Pen outlinePen;
What I try to do is to serialize List of Shape, but all I have isType
'System.Drawing.Pen' in Assembly 'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.
If i mark this field as [field: NonSerialized()]
then I can't use my loaded objects, because outlinePen is null.
Are there any other ways to serialize System.Drawing.Pen?
Upvotes: 3
Views: 2548
Reputation: 6886
Pen
is internally marked as non-serializable by .NET and it's sealed so you can't subclass and serialize it either. If you need to serialize graphics, you can look into the Metafile class which is specifically designed to store graphics and drawing objects.
Upvotes: 1
Reputation: 322
I would reccomend a different approach involving the execution of some logic when the parent object is deserialized.
[NonSerialized]
private Pen _penToUse;
[OnDeserialized()]
internal void Reinitialize(StreamingContext context)
{
SwitchPen(); //now we can restore the Pen, the easy way would be
//to store it in a static class in the domain
}
Upvotes: 0
Reputation: 62256
You should serialize only the data you interested in . Pen
is a graphic object , so even if it would be possible, imo, it's not a good idea to store it.
For example in your case you can store PenColor
, PenWidth
and PenStyle
like a PenDataObject
, which is kind of Pen
lighweght object specially created to store Pen
data.
Hope this helps.
Upvotes: 3