public static byte[] CompressMyObject(Map obj) { try { using (MemoryStream output = new MemoryStream()) { using (GZipStream zipStream = new GZipStream(output, CompressionMode.Compress)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(zipStream, obj); } return output.ToArray(); } } catch (Exception) { return null; } } public static Map DecompressMyObject(byte[] input) { try { using (MemoryStream output = new MemoryStream(input)) { using (GZipStream zipStream = new GZipStream(output, CompressionMode.Decompress)) { BinaryFormatter formatter = new BinaryFormatter(); return (formatter.Deserialize(zipStream) as Map); } } } catch (Exception) { return null; } } public static void Serialize(string url) { try { byte[] byteMap = CompressMyObject(myMap); Stream stream = File.Open(url, FileMode.Create); BinaryFormatter bFormatter = new BinaryFormatter(); bFormatter.Serialize(stream, byteMap); stream.Close(); bFormatter = null; } catch (InvalidOperationException e) { MessageBox.Show(e.ToString()); } } public static void Deserialize(string url) { try { byte[] byteMap; Stream stream = File.Open(url, FileMode.Open); BinaryFormatter bFormatter = new BinaryFormatter(); byteMap = (byte[])bFormatter.Deserialize(stream); myMap = DecompressMyObject(byteMap); stream.Close(); bFormatter = null; myMap.ResetMap(); UpdateCheckList(); } catch (InvalidOperationException e) { MessageBox.Show(e.ToString()); } }