I'm trying to properly save image that will be uploaded to picturebox by user and should be saved to database after clicking save button. For the line of code below it says image1 does not contain a definition for save.
Image1.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
data = ms.ToArray();
this is my code for uploading the image to picturebox which is (image1)
string folderPath = Server.MapPath("~/Files/");
//Check whether Directory (Folder) exists.
if (!Directory.Exists(folderPath))
{
//If Directory (Folder) does not exists Create it.
Directory.CreateDirectory(folderPath);
}
//Save the File to the Directory (Folder).
FileUpload1.SaveAs(folderPath + Path.GetFileName(FileUpload1.FileName));
//Display the Picture in Image control.
Image1.ImageUrl = "~/Files/" + Path.GetFileName(FileUpload1.FileName);
this is basic code I tried to insert into database
byte[] data;
using (MemoryStream ms = new MemoryStream())
{
Image1.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
data = ms.ToArray();
}
{
using (MySqlConnection con = new MySqlConnection(myconstring))
{
con.Open();
using (MySqlCommand command = new MySqlCommand("INSERT INTO image (picture) VALUES (@IM)", con))
{
command.Parameters.AddWithValue("@IM", data);
command.ExecuteNonQuery();
}
}
this is my code for selecting the image from the database to display in picture box which works fine when I insert picture directly into the database and not from the webpage.
byte[] bytes = (byte[])command.ExecuteScalar();
string strBase64 = Convert.ToBase64String(bytes);
if (strBase64 != null)
{
prvimage.ImageUrl = "data:Image/png;base64," + strBase64;
}
else
{
Response.Write("Must upload an Image");
}
Please help.