繁体   English   中英

将字节数组转换为图像

[英]Converting byte array to image

我们正在使用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转换为图像。

尽管找出我做错了什么,但我无法终生。

尝试此操作会发生什么:

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

暂无
暂无

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

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