String 如何将GIF动画转换为Base64字符串,然后再转换回GIF动画?

String 如何将GIF动画转换为Base64字符串,然后再转换回GIF动画?,string,base64,gif,String,Base64,Gif,我正在尝试编写代码,将GIF annimation文件转换为base64字符串,然后将其从base64字符串转换回图像。我写的代码是针对标准图像文件(如位图、JPEG、GIF)。然而,动画GIF是不同的,显然需要不同的步骤 if (pbTitlePageImage.Image != null) { // This is the step-by-step for writing an image to binary.

我正在尝试编写代码,将GIF annimation文件转换为base64字符串,然后将其从base64字符串转换回图像。我写的代码是针对标准图像文件(如位图、JPEG、GIF)。然而,动画GIF是不同的,显然需要不同的步骤

    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字符串的代码:

    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字符串转换回图像而编写的代码:

    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;
                    }
                }
            }
        {
            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字符串转换回图像后,必须将其加载到picturebox中。上面的代码示例将起作用,但只有动画链中的第一个图像才能通过,因此是出现在picturebox中的动画的唯一部分。我需要编写代码来处理整个动画。提前谢谢

我找到了解决这个问题的方法:

    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;
                    }
                }
            }
byte[] imageByte = Base64.decodeBase64(imageData.getImageBase64());
new FileOutputStream("image2.gif");
write(imageByte);
close();

我不知道为什么,但使用文件输出字符串我可以得到一个完整动画的文件。

这个问题可能部分地回答了这个问题:谢谢,丹,但我仍然需要更多信息。主要是,我需要知道动画GIF是否全部被转换为base 64字符串,以及在将其放入图片框之前如何将其完全传递到字节数组或类似类型。明白。我有一种感觉,这篇文章可能只解释了一些故事,如果有的话。您是否能够使用文件流打开整个文件并将其放入MemoryStream?Bitmap.FromStream将对其进行转换,但FileStream将读取原始字节。FileStream?我试试看。在尝试编写代码之前,我还需要知道是否应该首先确定.gif文件是单个图像还是动画。或者,只要文件扩展名是“.gif”,这有什么关系?如果只是使用FileStream而根本不解释它,那么你真的不需要了解下面的数据——也许只是为了确保文件实际上是gif文件。因此,如果您检查前四个字节并查看GIF8,您将可以处理99.9%的情况。
    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;
                    }
                }
            }