public static string ReadFromImage(Image image) { if (image == null) throw new ArgumentNullException(nameof(image)); using (var bdStream = new BitmapDataStream((Bitmap) image)) using (var gZipStream = new GZipStream(bdStream, CompressionMode.Decompress)) using (var memoryStream = new MemoryStream()) { gZipStream.CopyTo(memoryStream); return Encoding.UTF8.GetString(memoryStream.ToArray()); } }
public static void WriteToImage(string data, Image image) { if (data == null) throw new ArgumentNullException(nameof(data)); if (image == null) throw new ArgumentNullException(nameof(image)); using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(data))) using (var bdStream = new BitmapDataStream((Bitmap) image)) { bdStream.SetLength(0); using (var gZipStream = new GZipStream(bdStream, CompressionMode.Compress)) memoryStream.CopyTo(gZipStream); } }