Binary Serialization of Array

  • Thread starter news.microsoft.com
  • Start date
N

news.microsoft.com

I need a quick way to binary serialize an ArrayList of System.Drawing.Point
objects. There are a lot of point is this list so I am trying to avoid
iterating over them. I have tried the following but cannot find an easy way
to Deserialize the data. Can anyone suggest a better way?

Array ptArray = _allLines.ToArray(typeof(System.Drawing.Point));


System.IO.MemoryStream ms = new System.IO.MemoryStream();

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(ms,ptArray);


return ms.GetBuffer();
 
N

Nicholas Paldino [.NET/C# MVP]

To deserialize, you can take the byte array, and do this:

// Create a stream wrapping the byte array.
using (MemoryStream ms = new MemoryStream(buffer))
{
// Create a formatter.
IFormatter formatter = new BinaryFormatter();

// Deserialize.
Point[] points = (Point[]) formatter.Deserialize(ms);
}

Hope this helps.
 
T

TJO

Thank you so much for that very nice approach!!!!

Nicholas Paldino said:
To deserialize, you can take the byte array, and do this:

// Create a stream wrapping the byte array.
using (MemoryStream ms = new MemoryStream(buffer))
{
// Create a formatter.
IFormatter formatter = new BinaryFormatter();

// Deserialize.
Point[] points = (Point[]) formatter.Deserialize(ms);
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

news.microsoft.com said:
I need a quick way to binary serialize an ArrayList of
System.Drawing.Point objects. There are a lot of point is this list so I
am trying to avoid iterating over them. I have tried the following but
cannot find an easy way to Deserialize the data. Can anyone suggest a
better way?

Array ptArray = _allLines.ToArray(typeof(System.Drawing.Point));


System.IO.MemoryStream ms = new System.IO.MemoryStream();

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(ms,ptArray);


return ms.GetBuffer();
 

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