简体   繁体   English

将字节数组转换为图像

[英]Converting byte array to image

We are using Flash to send an image to the servers and upload an image. 我们正在使用Flash将图像发送到服务器并上传图像。 The way we're trying to do this is by sending the bytes to the server through a parameter and then converting the bytes to an image. 我们尝试执行此操作的方法是通过参数将字节发送到服务器,然后将字节转换为图像。

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

In the image the object is sending a few different bits of information. 在图像中,对象正在发送一些不同的信息。 I'm not sure if I am supposed to just receive one bit of information instead of the entire object. 我不确定是否应该只接收一点信息而不是整个对象。

So far I have the post request 到目前为止,我有职位要求

string byteArray = Request["bytes"];

Then I am trying to convert it to an image 然后我试图将其转换为图像

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

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

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

My helper method looks like - 我的助手方法看起来像-

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();
        }
    }
}

We've multiple different methods such as trying to convert it from Base64String to an image. 我们有多种方法,例如尝试将其从Base64String转换为图像。

I can't for the life of me though figure out what I am doing wrong. 尽管找出我做错了什么,但我无法终生。

What happens when you try this: 尝试此操作会发生什么:

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