繁体   English   中英

将Base64字符串转换为GIF图像

[英]Converting Base64 string to GIF image

对于辅助项目,我试图制作一个带有2个参数的简单可执行文件:

  1. 文本文件路径(包含Base64编码的动画GIF)(例如base.txt)
  2. 要导出的GIF的名称(示例hello.gif)

我认为应该全部采用的方式是:

  1. 它打开并从文件中读取数据
  2. 它获取数据并解码Base64字符串
  3. 然后将数据另存为动画GIF

当然,现在需要执行一些中间步骤,但是我认为目前无需覆盖。

所以最后我剩下了下面的C#文件,该文件应该可以执行我想要的操作。

using System;
using System.Drawing;
using System.Text;
using System.IO;

namespace Base642Img
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            if (args.Length == 2) {
                byte[] buffer;
                String imageData;
                FileStream fileStream = new FileStream (args [0], FileMode.Open, FileAccess.Read);

                try {
                    int length = (int)fileStream.Length;
                    buffer = new byte[length];
                    int count;
                    int sum = 0;

                    while ((count = fileStream.Read (buffer, sum, length - sum)) > 0) {
                        sum += count;
                    }
                } finally {
                    fileStream.Close();
                }

                try{
                    imageData = Encoding.UTF8.GetString (buffer, 0, buffer.Length);
                    imageData = imageData.Replace (System.Environment.NewLine, "");

                    byte[] imageBytes = System.Convert.FromBase64String (imageData);

                    MemoryStream ms = new MemoryStream (imageBytes, 0, imageBytes.Length);
                    ms.Write (imageBytes, 0, imageBytes.Length);

                    Image image = Image.FromStream (ms, true);
                    image.Save (args [1], System.Drawing.Imaging.ImageFormat.Gif);
                } catch (Exception e) {
                    Console.WriteLine (e.ToString ());
                }
            } else {
                Console.WriteLine ("Incorrect number of arguments");
            }
        }
    }
}

这在MonoDevelop中构建得很好。 但是,当我去运行文件时(考虑到两个参数),它会抛出以下异常:

System.FormatException: Invalid length.
  at (wrapper managed-to-native) System.Convert:InternalFromBase64String (string,bool)
  at System.Convert.FromBase64String (System.String s) [0x00000] in <filename unknown>:0 
  at Base642Img.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 

我完全不知道这是什么意思,我在Google上的所有搜索都变成空白,所以现在转向Stack Overflow,希望有人可以帮助我。

Convert.FromBase64String要求在输入字符串的末尾最多填充两个=字符,以便其总长度(忽略空格字符)为四的倍数。 显然,创建基本64位编码文件的程序省略了尾随填充。

您需要根据需要自己添加缺少的填充字符:

// using System.Collections.Generic;
// using System.Linq;

string imageData = File.ReadAllText(args[0]);
HashSet<char> whiteSpace = new HashSet<char> { '\t', '\n', '\r', ' ' };
int length = imageData.Count(c => !whiteSpace.Contains(c));
if (length % 4 != 0)
    imageData += new string('=', 4 - length % 4); // Pad length to multiple of 4.
byte[] imageBytes = Convert.FromBase64String(imageData);
MemoryStream ms = new MemoryStream(imageBytes);
Image image = Image.FromStream(ms, true);
image.Save(args[1], System.Drawing.Imaging.ImageFormat.Gif);

注意:由于Convert.FromBase64String忽略空格字符,因此不必删除换行符。 (我感谢EZI指出了这一点。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM