Binary Serialization of Large ArrayList

  • Thread starter Thread starter TJO
  • Start date Start date
T

TJO

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();
 
TJO 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));

Well that's creating a copy of the whole list to start with...
Iterating is likely to be much faster than that.
System.IO.MemoryStream ms = new System.IO.MemoryStream();

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(ms,ptArray);

return ms.GetBuffer();

Rather than using GetBuffer, you should use ToArray - otherwise you'll
end up with it being 0-padded at the end. That may well be your current
problem.
 
This will serialize a point[] array pretty much just as you have into a
memory stream, then deserialize it back into a point array.

{
Point[] ptArray = { new Point(1,1), new Point (2,2), new Point(3,3)};
//Or Point[] ptArray = (Point[])_allLines.ToArray(typeof(Point))

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

formatter.Serialize(ms, ptArray);
ptArray = null;


//Deserialize:
ms.Seek(0, System.IO.SeekOrigin.Begin);
ptArray = (Point[])formatter.Deserialize(ms);
}
 
Back
Top