简体   繁体   English

C#字节数组到图像

[英]C# byte array to Image

I need to create an Image from a Byte Array but I don't know how to do this. 我需要从字节数组创建图像,但是我不知道该怎么做。 I tried to do it like this: 我试图这样做:

using (var ms = new MemoryStream(byteArrayIn))
{
    return Image.FromStream(ms);
}

But there was always the message that the parameter ms is not valid. 但是总有消息说参数ms无效。 The exact Exception message is: 确切的异常消息是:

 An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll

With this I am reading the Array from the database 这样我正在从数据库中读取数组

byte[] bytes = ObjectToByteArray(reader["profilepicture"]);

private byte[] ObjectToByteArray(Object obj)
    {
        if (obj == null)
            return null;
        BinaryFormatter bf = new BinaryFormatter();
        using (MemoryStream ms = new MemoryStream())
        {
            bf.Serialize(ms, obj);
            return ms.ToArray();
        }
    }

Can anybody please help me with this problem? 有人可以帮我解决这个问题吗?

Try this: 尝试这个:

public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}

Source 资源

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

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