1

I´ve stream video on a pictureBox control, and I want to get image from PictureBox, to store on a table on SQL Server. PictureBox show stream but, PictureBox retrieve null.

What is wrong?

private void button1_Click(object sender, EventArgs e){
        SqlConnection con = new SqlConnection("Data Source=IBM-PC\\SQLEXPRESS2;Initial Catalog=DBACCESS;Integrated Security=True");

        if (cmrConductor.Image == null){
            mensajeOK("Error");
        }else{
            MemoryStream ms = new MemoryStream();
            cmrConductor.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] Pic_arr = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(Pic_arr, 0, Pic_arr.Length);

            SqlCommand cmd = new SqlCommand("INSERT INTO tblUsers (fldCode, fldPic) VALUES (@fldCode, @fldPic)", con);
            cmd.Parameters.AddWithValue("@fldCode", txtId.Text);
            cmd.Parameters.AddWithValue("@fldPic", Pic_arr);
            con.Open();
            try{
                int res = cmd.ExecuteNonQuery();
                if (res > 0){
                    MessageBox.Show("insert");
                }
            }
            catch (Exception ex){
                MessageBox.Show(ex.Message);
            }
            finally{
                con.Close();
            }
        }
    }
Leinad
  • 111
  • 10
  • Your question is not at all clear. It is nearly impossible for anybody to offer a solution here because we don't have enough details to work with. Slightly off topic but you should read this article about AddWithValue. http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ – Sean Lange Sep 19 '16 at 14:38
  • I know it´s complicated to explain, actually I´m a beginner developer. I just want to capture the image (or snapshot) from a stream video from a PictureBox control and store on SQL Server. The code above works or detects when control isn't null, but PictureBox don't recognizes the video image, I don't know. – Leinad Sep 19 '16 at 14:56
  • 1
    Personally I would store image on disc and the path in sql server. Storing images directly in the database has some performance challenges. At the very least I would store the images in their own table instead of as a column in the Users table. As far as your code there is no chance we can help because you still haven't provided any details. – Sean Lange Sep 19 '16 at 15:36
  • @Sean-Lange Well,I needed store the image on SQL Server, because I want avoid saving through a route. Indeed, the image isn't big, the size is 100 KB. Thank you anyway. – Leinad Sep 20 '16 at 13:50

2 Answers2

1

You need to set your image field type as varbinary(MAX) and you need to convert your image into a byte array before inserting.

//Insert image
SqlCommand comm = new SqlCommand("Insert into ImageColumn values (@Image)")
comm.Parameters.AddWithValue("@Image",  Converter.GetBytes(pictureBox.image));


//Retrieving image
pictureBox1.Image = Converter.GetImage(dataTable.Rows[0]["ImageColumn"])


//Converter class
class Converter
{
    public static byte[] GetBytes(System.Drawing.Image imageIn)
    {
        using (var ms = new MemoryStream())
        {
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            return ms.ToArray();
        }
    }
    public static byte[] GetBytes(string path)
    {
        using (var ms = new MemoryStream())
        {
            Image img = Image.FromFile(path);
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            return ms.ToArray();
        }
    }
    public static Image GetImage(byte[] buffer)
    {
        using (var ms = new MemoryStream(buffer))
        {
            return Image.FromStream(ms);
        }
    }
}
0

Here is the solution, thanks to this post Saving Panel as an Image. I just had to change PictureBox control by a Panel control.

private void button1_Click(object sender, EventArgs e){        
        SqlConnection con = new SqlConnection("Data Source=IBM-PC\\SQLEXPRESS2;Initial Catalog=DBACCESS;Integrated Security=True");

        MemoryStream ms = new MemoryStream();

        Bitmap bmp = new Bitmap(cmrConductor.Width, cmrConductor.Height);
        cmrConductor.DrawToBitmap(bmp, cmrConductor.Bounds);
        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

        byte[] Pic_arr = new byte[ms.Length];
        ms.Position = 0;
        ms.Read(Pic_arr, 0, Pic_arr.Length);

        SqlCommand cmd = new SqlCommand("INSERT INTO tblUsers (fldCode, fldPic) VALUES (@fldCode, @fldPic)", con);

        cmd.Parameters.AddWithValue("@fldCode", txtId.Text);
        cmd.Parameters.AddWithValue("@fldPic", Pic_arr);
        con.Open();
        try
        {
           int res = cmd.ExecuteNonQuery();
           if (res > 0)
           {
              MessageBox.Show("insert");
           }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }
Community
  • 1
  • 1
Leinad
  • 111
  • 10