Binary blob serialization

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top