【问题标题】:Converting byte array to image将字节数组转换为图像
【发布时间】:2015-08-16 09:57:07
【问题描述】:

我们正在使用 Flash 将图像发送到服务器并上传图像。我们尝试这样做的方法是通过参数将字节发送到服务器,然后将字节转换为图像。

http://i.gyazo.com/fb8225af80ef465b0262d97f63bd54b2.png

在图像中,对象正在发送一些不同的信息。我不确定我是否应该只接收一点信息而不是整个对象。

到目前为止,我有发帖请求

string byteArray = Request["bytes"];

然后我正在尝试将其转换为图像

string file_name = Guid.NewGuid().ToString();

//byte[] imageBytes = Convert.FromBase64String(byteArray);

Derio.App.Model.Helper.ByteArrayToFile(file_name, Derio.App.Model.Helper.GetBytes(byteArray));

我的辅助方法看起来像 -

public static class Helper
{
    public static byte[] GetBytes(string str)
    {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }

    public static string ByteArrayToFile(string _FileName, byte[] _ByteArray)
    {
        try
        {
            // Open file for reading
            System.IO.FileStream _FileStream =
               new System.IO.FileStream(_FileName, System.IO.FileMode.Create,
                                        System.IO.FileAccess.Write);
            // Writes a block of bytes to this stream using data from
            // a byte array.
            _FileStream.Write(_ByteArray, 0, _ByteArray.Length);

            // close file stream
            _FileStream.Close();

            return "true";
        }
        catch (Exception _Exception)
        {
            // Error
            return "Exception caught in process:" +     _Exception.ToString();
        }
    }
}

我们有多种不同的方法,例如尝试将其从 Base64String 转换为图像。

我终其一生都无法弄清楚我做错了什么。

【问题讨论】:

  • 你还没有告诉我们问题出在哪里,首先。 (我一点也不喜欢你在ByteArrayToFile 中“处理”异常的方式,但那是另一回事。)
  • 你试过Image.FromStream(memory_stream);吗?
  • 神圣的异常处理程序,蝙蝠侠! return "true";?!真的吗?
  • 我只是这样返回它,所以我可以将错误返回到设备,不是永久放置
  • 您遇到的错误究竟是什么?在哪一行?请您用这些信息更新您的问题吗?

标签: c# image byte server bytearray


【解决方案1】:

当你尝试这个时会发生什么:

string file_name = Guid.NewGuid().ToString();
string byteArray = Request["bytes"];
byte[] imageBytes = Convert.FromBase64String(byteArray);
IO.File.WriteAllBytes(file_name, imageBytes);

【讨论】:

  • 我收到错误 - System.Convert.FromBase64_Decode(Char startInputPtr, Int32 inputLength, Byte startDestPtr, Int32 destLength)\r\n 在 System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)\ r\n 在 System.Convert.FromBase64String(String s)
  • 这是错误/异常的 stacktrace,即异常发生的哪里。但是异常类型和消息是什么?
  • 消息是:输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或填充字符中的非法字符。
  • byteArray 看起来像什么?你能把它的前几个字符贴在这里吗?另外,当您将我的代码示例中的第 3 行与 byte[] imageBytes = Derio.App.Model.Helper.GetBytes(byteArray) 交换时会发生什么?
  • 上传成功,但不是图片,是“Windows照片查看器打不开这张图片,如果我用记事本打开,有文字“ÿØÿà”,我也发了一张图片byteArray 是什么
猜你喜欢
  • 1970-01-01
  • 2013-05-19
  • 2013-01-14
  • 2013-07-29
  • 2013-08-31
  • 2011-05-17
相关资源
最近更新 更多