将字节数组转换为图像

本文关键字:图像 转换 数组 字节 字节数 | 更新日期: 2023-09-27 17:52:48

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

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

在图像中,对象发送了几个不同的信息位。我不确定我是否应该只接收一个比特的信息,而不是整个对象。

到目前为止,我有post request

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转换为图像。

我怎么也想不出我到底做错了什么

将字节数组转换为图像

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

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