我有两种方法,一种是将图像转换为Base64字符串以便在XML标签中存储它,另一种是将Base64字符串转换回图像。我能够将图像转换为字符串并将其存储在XML中,但是当我尝试将字符串转换回图像时,出现以下错误:“GZip头中的魔数不正确。请确保您正在传递一个GZip流。” 有什么解决方法吗?
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;
}