views:

502

answers:

1

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;
}
+3  A: 

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.

Guffa
How would I know the size of the unzipped data? Do I have to create a random amount of byte size that I think would be adequate for the decompression? I'm dealing with images so that number can be pretty large.
Icono123
@Icono123: Check if you can ask the GZipStream for the size, otherwise the only option is to store the original size along with the data. If you can't do that, you have to allocate more data as you read the stream. You could use a MemoryStream to do that for you.
Guffa