如何将GIF动画转换为Base64字符串并转换回GIF动画? [英] How Do I Convert A GIF Animation To Base64 String And Back To A GIF Animation?

查看:1575
本文介绍了如何将GIF动画转换为Base64字符串并转换回GIF动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写代码,将GIF动画文件转换为base64字符串,然后将其从base 64字符串转换回图像.我编写的代码是针对标准图片文件(即位图,JPEG,GIF)的.但是,动画GIF有所不同,显然需要逐步进行.

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.

这是我编写的用于将图像转换为base64字符串的代码:

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;
                    }
                }
            }

这是我编写的将base64字符串转换回图像的代码:

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;
        }

将base64字符串转换回图像后,必须将其加载到图片框.上面的代码示例将起作用,但是只有动画链中的第一张图像才能通过,因此,这是出现在图片框中的动画的唯一部分.我需要编写处理整个动画的代码.预先感谢!

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!

推荐答案

我找到了一种解决方法:

I found a way to solve it:

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

我不知道为什么,但是使用FileOutput String可以得到带有完整动画的文件.

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

这篇关于如何将GIF动画转换为Base64字符串并转换回GIF动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆