convert binary to bitmap using memory stream

前端 未结 3 966
执念已碎
执念已碎 2020-12-10 04:19

Hi I wanna convert binary array to bitmap and show image in a picturebox. I wrote the following code but I got exception that says that the parameter is not val

3条回答
  •  失恋的感觉
    2020-12-10 04:44

    It really depends on what is in blob. Is it a valid bitmap format (like PNG, BMP, GIF, etc?). If it is raw byte information about the pixels in the bitmap, you can not do it like that.

    It may help to rewind the stream to the beginning using mStream.Seek(0, SeekOrigin.Begin) before the line Bitmap bm = new Bitmap(mStream);.

    public static Bitmap ByteToImage(byte[] blob)
    {
        using (MemoryStream mStream = new MemoryStream())
        {
             mStream.Write(blob, 0, blob.Length);
             mStream.Seek(0, SeekOrigin.Begin);
    
             Bitmap bm = new Bitmap(mStream);
             return bm;
        }
    }
    

提交回复
热议问题