I have two methods, one that I use to convert an image to a Base64 string so I can store it in an XML tag and another to convert the Base64 string back to an image. I'm able to convert the image to a string and store it in the XML, but when I try to convert the string back to an image I'm getting the following error: "The magic number in GZip header is not correct. Make sure you are passing in a GZip stream."
Any thoughts on how to resolve this?
public static string ConvertToBase64String(Image Image, ImageFormat Format)
{
MemoryStream stream = new MemoryStream();
Image.Save(stream, Format);
byte[] imageBytes = stream.ToArray();
MemoryStream memStream = new MemoryStream();
GZipStream zipStream = new GZipStream(memStream, CompressionMode.Compress);
zipStream.Write(imageBytes, 0, imageBytes.Length);
string imageString = Convert.ToBase64String(imageBytes);
stream.Close();
memStream.Close();
return imageString;
}
public static Image Base64StringToImage(string ImageArray)
{
byte[] base64String = Convert.FromBase64String(ImageArray);
MemoryStream memStream = new MemoryStream(base64String);
GZipStream zipStream = new GZipStream(memStream, CompressionMode.Decompress);
zipStream.Read(base64String, 0, base64String.Length);
ImageConverter ic = new ImageConverter();
Image image = (Image)ic.ConvertFrom(base64String);
memStream.Close();
return image;
}