I have a problem saving images from PictureBox1 to my SQL server database, I\'ve done my research and found out that I have to convert my image to a byte array in order to d
You just need to define an image path named "imgpath"
//upload image
private void button1_Click_1(object sender, EventArgs e)
{
OpenFileDialog opf = new OpenFileDialog();
opf.Filter = "Choose Image(*.jpg; *.png; *.gif)|*.jpg; *.png; *.gif";
if (opf.ShowDialog() == DialogResult.OK)
{
imgpath.Text = opf.FileName;
pictureBox3.Image = Image.FromFile(opf.FileName);
}
}
// ADD NEW/INSERT IMAGE TO DB
private void button5_Click(object sender, EventArgs e)
{
string filepath = imgpath.Text;
string filename = Path.GetFileName(imgpath.Text);
FileStream fs = new FileStream(imgpath.Text, FileMode.Open,
FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();
if (imgpath.Text != "")
{
cmd = new SqlCommand("insert into tbl_Record(@StudentImage)", con);
con.Open();
MemoryStream ms = new MemoryStream();
// must use toolbox to add picturebox
pictureBox3.Image.Save(ms, pictureBox3.Image.RawFormat);
byte[] img = ms.ToArray();
cmd.Parameters.Add("@StudentImage", SqlDbType.Binary).Value =
bytes;
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Succeed");
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Fill Required Informations!");
}
}