将图像存储在SQL Server上,从PictureBox控件上的流中存储

时间:2016-09-19 13:54:46

标签: c# sql-server winforms

我在pictureBox控件上传输视频,我想从PictureBox获取图像,以存储在SQL Server上的表上。 PictureBox显示流但是,PictureBox检索null。

有什么问题?

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

2 个答案:

答案 0 :(得分:1)

您需要将图像字段类型设置为varbinary(MAX),并且需要在插入之前将图像转换为字节数组。

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

答案 1 :(得分:0)

这是一个解决方案,感谢这篇文章Saving Panel as an Image。我只需要通过Panel控件更改PictureBox控件。

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