Binary Serialization of Array

  • Thread starter Thread starter news.microsoft.com
  • Start date 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();
 
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.
 
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();
 
Back
Top