1

I'm trying to write code to convert a GIF annimation file into a base64 string and then convert it from base 64 string back into an image. The code I've written is for standard image files (i.e. Bitmap, JPEG, GIF). Animated GIFs, however, are different and obviously require a different step-by-step.

Here is the code I've written for converting an image to base64 string:

    if (pbTitlePageImage.Image != null)
            {
                // This is the step-by-step for writing an image to binary.
                string Image2BConverted;
                using (Bitmap bm = new Bitmap(pbTitlePageImage.Image))
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        BinaryWriter bw = new BinaryWriter(ms);
                        bm.Save(ms, ImageFormat.Jpeg);
                        Image2BConverted = Convert.ToBase64String(ms.ToArray());
                        GameInfo.TitlePageImage = Image2BConverted;
                        bw.Close();
                        ms.Close();
                        GameInfo.TitlePageImagePresent = true;
                        ProjectNeedsSaving = true;
                    }
                }
            }

And here is the code I've written for converting a base64 string back to an image:

        {
            byte[] TitlePageImageBuffer = Convert.FromBase64String(GameInfo.TitlePageImage);
            MemoryStream memTitlePageImageStream = new MemoryStream(TitlePageImageBuffer, 0, TitlePageImageBuffer.Length);
            memTitlePageImageStream.Write(TitlePageImageBuffer, 0, TitlePageImageBuffer.Length);
            memTitlePageImageStream.Position = 0;

            pbTitlePageImage.Image = Bitmap.FromStream(memTitlePageImageStream, true);

            memTitlePageImageStream.Close();
            memTitlePageImageStream = null;
            TitlePageImageBuffer = null;
        }

After converting the base64 string back into an image, it must be loaded into a picturebox. The above code examples will work, but only the first image in the animation chain makes it through and thus is the only part if the animation that appears in the picturebox. I need to write code that will handle the entire animation. Thanks in advance!

user2272380
  • 103
  • 1
  • 2
  • 13
  • This question may answer this partially: http://stackoverflow.com/questions/8763630/c-sharp-gif-image-to-memorystream-and-back-lose-animation – allen1 Aug 27 '14 at 20:32
  • Thanks, Dan, but I still need more info. Mainly, I need to know if the animated GIF is being converted to base 64 string in its entirety and how to pass it entirely into a byte array or similar type before putting it into a picture box. – user2272380 Aug 27 '14 at 22:40
  • Understood. I had a feeling that post may only explain some of the story, if any at all. Are you able to use a FileStream to open the entire file and place it into a MemoryStream? Bitmap.FromStream will convert it, but a FileStream will read the bytes raw. – allen1 Aug 28 '14 at 14:19
  • FileStream? I'll try it. Before I try writing the code, I also need to know if I should first determine if the .gif file is either a single image or an animation. Or does it matter, so long as it's file extension is ".gif"? – user2272380 Aug 28 '14 at 14:46
  • If just using FileStream and not interpreting it at all, you really don't need to know anything about the data underneath - except maybe to ensure that the file actually IS a gif. For that, if you check the first four bytes and see GIF8, you'll be okay for 99.9% of cases. – allen1 Aug 28 '14 at 14:51

1 Answers1

0

I found a way to solve it:

byte[] imageByte = Base64.decodeBase64(imageData.getImageBase64());
new FileOutputStream("image2.gif");
write(imageByte);
close();

I dont know why, but using a FileOutput String i could get a file with the complete animation.

Martin Larizzate
  • 702
  • 1
  • 12
  • 30