Binary blob serialization

  • Thread starter Thread starter Atul
  • Start date Start date
A

Atul

I cant figure out how to serialize and later deserialize
a binary chunk of data. For example how can I serialize a
bitmap image. I tried to find out how the Image class in
the framework acheives this but I could not.

For example, say I have a class

[Serializable]
public class Sample : ISerializable
{

Byte blob[]; // this byte array can be of variable length
....
....
};

Currently, the following is the only way apparent to me
to acheive this.
In the GetObjectData funcion, I encode the byte array to
a string and then use the AddValue function to add the
string.
During derialization, I call GetString to retrieve the
string and decode it to get the original byte array.

How can I serialize a byte array directly without
converting it to an encoded string first? ( I see no
overloads which take a byte array)

Thanks
Atul
 
Atul,
The byte array itself is serializable, the following should work:

[Serializable]
public class Sample
{

Byte blob[]; // this byte array can be of variable length

};

If you really need ISerializable, use the AddValue that accepts object, then
use the GetValue giving Byte array as the type.
[Serializable]
public class Sample : ISerializable
{

Byte blob[]; // this byte array can be of variable length
...
public Sample(SerializationInfo info, StreamingContext context)
{
blob = (Byte[])info.GetValue("blob", typeof(Byte[]));
}

public void GetObjectData(SerializationInfo info, StreamingContext
context)
{
info.AddValue("blob", blob);
}

Hope this helps
Jay

Atul said:
I cant figure out how to serialize and later deserialize
a binary chunk of data. For example how can I serialize a
bitmap image. I tried to find out how the Image class in
the framework acheives this but I could not.

For example, say I have a class

[Serializable]
public class Sample : ISerializable
{

Byte blob[]; // this byte array can be of variable length
...
...
};

Currently, the following is the only way apparent to me
to acheive this.
In the GetObjectData funcion, I encode the byte array to
a string and then use the AddValue function to add the
string.
During derialization, I call GetString to retrieve the
string and decode it to get the original byte array.

How can I serialize a byte array directly without
converting it to an encoded string first? ( I see no
overloads which take a byte array)

Thanks
Atul
 
Back
Top