[Solved] ASP.Net Error: image1 does not contain a definition for save

Last Reply 4 months ago By dharmendr

Posted 4 months ago

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.

Posted 4 months ago

Hi milepredy,

Refer the below code. 

First you need to convert the image url to Bitmap image then use the Bitmap Save method to assign it to the MemoryStream and convert it to byte array.

C#

protected void btnUpload_Click(object sender, EventArgs e)
{
    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);
}
protected void btnInsert_Click(object sender, EventArgs e)
{
    try
    {
        byte[] data;
        using (MemoryStream ms = new MemoryStream())
        {
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(Server.MapPath(Image1.ImageUrl));
            bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            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();
            }
        }
    }
    catch (Exception ex)
    {
    }
}

VB.Net

    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim folderPath As String = Server.MapPath("~/Files/")

        If Not Directory.Exists(folderPath) Then
            Directory.CreateDirectory(folderPath)
        End If

        FileUpload1.SaveAs(folderPath & Path.GetFileName(FileUpload1.FileName))
        Image1.ImageUrl = "~/Files/" & Path.GetFileName(FileUpload1.FileName)
    End Sub

    Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As EventArgs)
        Try
            Dim data As Byte()

            Using ms As MemoryStream = New MemoryStream()
                Dim bitmap As System.Drawing.Bitmap = New System.Drawing.Bitmap(Server.MapPath(Image1.ImageUrl))
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
                data = ms.ToArray()
            End Using

            Using con As MySqlConnection = New MySqlConnection(myconstring)
                con.Open()

                Using command As MySqlCommand = New MySqlCommand("INSERT INTO image (picture) VALUES (@IM)", con)
                    command.Parameters.AddWithValue("@IM", data)
                    command.ExecuteNonQuery()
                End Using
            End Using

        Catch ex As Exception
        End Try
    End Sub