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

后端 未结 2 712
执笔经年
执笔经年 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();
                }
            }
        }
    
    0 讨论(0)
  • 2021-01-12 07:38

    I just recently had to do the exact same thing in VB.NET. Here is my code, run through Telerik's Code Converter. It was quite tricky to get working, and is still honestly a pain.

    To upload an image:

    private bool uploadImage(ref Bitmap p)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = Configuration.ConfigurationManager.ConnectionStrings("ConnStringHere").ConnectionString;
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "INSERT INTO Table_Name (File2) VALUES (@File2)"; //I named the column File2 simply because "File" seemed to be a keyword in SQLServer
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
    
        SqlParameter File1 = new SqlParameter("@File2", SqlDbType.Image);
        MemoryStream ms = new MemoryStream();
    
        using (Bitmap tempImage = new Bitmap(p))
        {
            tempImage.Save(ms, p.RawFormat);
        }
    
        byte[] data = ms.GetBuffer();
        if (!isValidImage(data)) //optional, will include code if requested.
        {
            return false;
        }
        File1.Value = data;
        cmd.Parameters.Add(File1);
    
        con.Open();
        int result = cmd.ExecuteNonQuery();
        if (result > 0)
        {
            // SUCCESS!
            con.Close();
            return true;
        }
        else
        {
            //failure
            con.Close();
            return false;
        }
    
    }
    

    To retrieve an image:

    private Bitmap retrieveBitmap()
        {
            Image image1 = null
            if (dt1.Rows.Count > 0)
            {
                byte[] imageData1 = null;
                if (dt1[0].Count > 0)
                {
                    if (!Information.IsDBNull(dt1.CopyToDataTable()[0].Item("File2")))
                    {
                        imageData1 = (byte[])dt1.CopyToDataTable()[0].Item("File2");
                    }
                }
                if ((imageData1 != null))
                {
                    if (isValidImage(imageData1))
                    {
                        using (MemoryStream ms = new MemoryStream(imageData1, 0, imageData1.Length))
                        {
                            ms.Write(imageData1, 0, imageData1.Length);
                            image1 = Image.FromStream(ms, true);
                        }
                        return image1;
                    }
                    else
                    {
                        // "Invalid image on server";
                        return null;
                    }
                }
            }
        }
    

    My code may need small formatting changes, please edit whatever has invalid syntax (my C# is a little rusty, and my code was run through a converter).

    0 讨论(0)
提交回复
热议问题