开发者

Error: The magic number in GZip header is not correct

开发者 https://www.devze.com 2022-12-26 18:17 出处:网络
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

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(imageB开发者_高级运维ytes, 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;
}


I see several errors in the code.

The problem that is causing the error message is that what you are not converting to a base 64 string is not the zipped data (memStream.ToArray()), but the data that you wrote to the zip stream (imageBytes). When you try to unzip the data that is not zipped, you get the error message.

Another major problem is that you are only reading part of the data from the zip stream, as you are using the size of the zipped data as size for how much to read from the zip stream.

Also, you are ignoring the result from the Read method. It returns the number of bytes that was actually placed in the array, which can be smaller than the number of bytes requested. As the Read method doesn't have to return all available data, you have to loop until you have actually got all data from the stream. The Read method will return zero when there is no more data to read.

Another problem is that you are writing to the array that you are using as back end for the memory stream that you are reading from. As the unzipped data generally is larger than the zipped data, you will be overwriting the data faster than you can read it. However, as the unzipped data won't fit in the array, you can't use it for that anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号