簡體   English   中英

C#字節數組到圖像

[英]C# byte array to Image

我需要從字節數組創建圖像,但是我不知道該怎么做。 我試圖這樣做:

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

但是總有消息說參數ms無效。 確切的異常消息是:

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

這樣我正在從數據庫中讀取數組

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

有人可以幫我解決這個問題嗎?

嘗試這個:

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

資源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM