Serialization problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I would like to serialize an instance of the bitmap instance. Actually, I've
done serialization with the code below, but why parameter Tag is not
serialized? I get the following exception after deserialization: Object
reference is not set to an instance of an object.


// Instance of bitmap.
Bitmap B = new Bitmap(100, 100);
// Add additional property to the bitmap.
B.Tag = 100;

// Serialize.
FileStream fs = new FileStream("test.dat", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, B);
fs.Close();

// Deserialize.
FileStream fs2 = new FileStream("test.dat", FileMode.Open);
BinaryFormatter bf2 = new BinaryFormatter();
Bitmap B2 = (Bitmap)bf2.Deserialize(fs2);
fs2.Close();

int tag = (int)B2.Tag;

All I want is to add the additional property to the bitmap instance, which
must be serialized. Is there any other way to do that?

Thank you.

Regards
Saso
 
Saso,

In Bitmap basclass Tag might have been marked as non serialiable. What
you can do , Create your own bitmap class derived from Bitmap and override
Tag property or create your own property to store your variable. Dont forget
to mark your class with [Serializable()] attribute.

Thanks
Baski
 
Baski,

I already tryed to derive from bitmap class, but unfortunately it can not be
derived from sealed type such System.Drawing.Bitmap is. Any suggestion?

Regards.

Saso

Baski said:
Saso,

In Bitmap basclass Tag might have been marked as non serialiable. What
you can do , Create your own bitmap class derived from Bitmap and override
Tag property or create your own property to store your variable. Dont forget
to mark your class with [Serializable()] attribute.

Thanks
Baski

Saso said:
Hi,

I would like to serialize an instance of the bitmap instance. Actually,
I've
done serialization with the code below, but why parameter Tag is not
serialized? I get the following exception after deserialization: Object
reference is not set to an instance of an object.


// Instance of bitmap.
Bitmap B = new Bitmap(100, 100);
// Add additional property to the bitmap.
B.Tag = 100;

// Serialize.
FileStream fs = new FileStream("test.dat", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, B);
fs.Close();

// Deserialize.
FileStream fs2 = new FileStream("test.dat", FileMode.Open);
BinaryFormatter bf2 = new BinaryFormatter();
Bitmap B2 = (Bitmap)bf2.Deserialize(fs2);
fs2.Close();

int tag = (int)B2.Tag;

All I want is to add the additional property to the bitmap instance, which
must be serialized. Is there any other way to do that?

Thank you.

Regards
Saso
 
Back
Top