Click here to Skip to main content
14,268,557 members
Rate this:
Please Sign up or sign in to vote.
See more:
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();


What I have tried:

//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

try
          {

              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");
             }
Posted
Updated 11-Aug-19 9:27am
v3
Rate this:
Please Sign up or sign in to vote.

Solution 2

The Image control[^] does not contain a Save method. But you don't need one - it's displaying an image from your server's disk.
string fileName = Server.MapPath(Image1.ImageUrl);
if (string.IsNullOrEmpty(fileName))
{
    // TODO: Display an error to the user
    return;
}

byte[] data = File.ReadAllBytes(fileName);

using (MySqlConnection con = new MySqlConnection(myconstring))
using (MySqlCommand command = new MySqlCommand("INSERT INTO image (picture) VALUES (@IM)", con))
{
    command.Parameters.AddWithValue("@IM", data);
    
    con.Open();
    command.ExecuteNonQuery();
}
NB: You'll want to validate that the uploaded file is actually an image. You'll also want to check that the filename provided by the user doesn't contain any characters which aren't valid in a Windows filename. Never trust user input! :)
   
Comments
milepredy 13-Aug-19 6:36am
   
Thank you so much.
Rate this:
Please Sign up or sign in to vote.

Solution 1

No, that won't work: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it's exactly the same problem you are having, except you will need to convert the byte array to a Base64 string before you save it.
   
Comments
milepredy 5-Aug-19 6:45am
   
I've been to that page, you can even tell from the similar parameters. It doesn't work for me as I get error with
image1.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
data = ms.ToArray();

which say's image1 does not contain definition for Save.
milepredy 5-Aug-19 6:45am
   
the solution on that page is one of the many solutions I have tried.
most of the solutions are desktop based applications. This is a Web app...
Rate this:
Please Sign up or sign in to vote.

Solution 3

SqlCommand cmd = new SqlCommand(") values()", cnn);
                cmd.CommandType = CommandType.Text;

 cnn.Open();

if (pictureFace.Image != null)
                    {
                        ms = new MemoryStream();
                        pictureFace.Image.Save(ms, ImageFormat.Jpeg);
                        byte[] photo_aray = new byte[ms.Length];
                        ms.Position = 0;
                        ms.Read(photo_aray, 0, photo_aray.Length);
                        cmd.Parameters.AddWithValue("@Image", photo_aray);
                    }
                    
                    cmd.ExecuteNonQuery();
}
   
Comments
milepredy 13-Aug-19 6:36am
   
Thank you so much.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100