Click here to Skip to main content
16,012,759 members
Please Sign up or sign in to vote.
2.11/5 (2 votes)
See more:
i just wanted to know if it is possible to save images into the database without the image path like for example a default image already assigned into the picture box. This is the sample code that I used that requires an image location. I want to set a default image into the picture box. The Image column in MySql is set to Blob. Thanks to those who will answer.
C#
byte[] imageBt = null;
FileStream fstream = new FileStream(this.txtImage.Text, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
imageBt = br.ReadBytes((int)fstream.Length);

conn.Open();
MySqlCommand command = new MySqlCommand("insert into images(image)values(@IMG)", conn);
command.Parameters.Add(new MySqlParameter("@IMG", imageBt));

reader = command.ExecuteReader();
MessageBox.Show("Saved");

while (reader.Read())
{

}
Posted
Updated 20-Sep-14 8:01am
v2
Comments
Sergey Alexandrovich Kryukov 20-Sep-14 14:08pm    
Please search CodeProject first...
—SA

I have found the solution to my question. Thank you to those who have answered . . .
C#
MemoryStream ms = new MemoryStream();
           pictureBox1.Image.Save(ms, ImageFormat.Png);
           byte[] pic_arr = new byte[ms.Length];
           ms.Position = 0;
           ms.Read(pic_arr, 0, pic_arr.Length);
           conn.Open();
           MySqlCommand cmd = new MySqlCommand("insert into images(image, name)values(@img,@imgName)", conn);
           cmd.Parameters.AddWithValue("@imgName", txtName.Text);
           cmd.Parameters.AddWithValue("@img", pic_arr);
           int n = cmd.ExecuteNonQuery();
           conn.Close();
           MessageBox.Show("COmplete");
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900