Saving an Image file to sql Server and converting byte array into image

后端 未结 2 734
执笔经年
执笔经年 2021-01-12 07:07

I am storing images in a database and would like to convert them from byte array to image. I have no problem converting an object to byte array but I get an error of \"Param

2条回答
  •  粉色の甜心
    2021-01-12 07:29

    Try to deserialize the object first from byte array with your BinaryFormatter!

    Try to use following two methods:

    private System.Drawing.Image ObjToImg(byte[] obj)
        {
            if (obj == null)
                return null;
            else
            {
                BinaryFormatter bf = new BinaryFormatter();
                using(MemoryStream ms = new MemoryStream(obj))
                {
                  return (System.Drawing.Image)bf.Deserialize(ms);
                }
            }
        }
    private byte[] ImgToObj(System.Drawing.Image obj)
        {
            if (obj == null)
                return null;
            else
            {
                BinaryFormatter bf = new BinaryFormatter();
                using(MemoryStream ms = new MemoryStream())
                {
                  bf.Serialize(ms, obj);
                  return ms.ToArray();
                }
            }
        }
    

提交回复
热议问题