alex dave
alex dave

Reputation: 115

How to save images in sql server using winform?

i am trying to save images like bmp,jpg,gif & png into sql server database. but not able to save all the formats into database. Only png image is getting saved into database. if trying to save jpeg, .bmp & .gif images, it's showing error "A generic error occured in GDI+". What is the problem?

private void InitializeOpenFileDialog()
{
 try
    {
      this.openFileDialog1 = new OpenFileDialog();

       // Set the file dialog to filter for graphics files. 
       this.openFileDialog1.Filter = "Images (*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|" + "All files (*.*)|*.*";
       //"image files|*.jpg;*.png;*.gif;*.bmp;.*;";
       // Allow the user to select multiple images. 
       this.openFileDialog1.Multiselect = true;
       this.openFileDialog1.Title = "My Image Browser";
      }
  catch(Exception es){
       MessageBox.Show(es.Message);
                   }

  }

//load picture
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }
 private void button2_Click(object sender, EventArgs e)
        {             

         try
          {
           MemoryStream ms1 = new MemoryStream();

           pictureBox2.Image.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
           // byte[] img_arr1 = ms1.ToArray();
          byte[] img_arr1 = new byte[ms1.Length];
          ms1.Read(img_arr1, 0, img_arr1.Length);
          SqlConnection con = new SqlConnection(@"data source=xdfgh\ALEXDAVE;database=x1234;UID=sa;password=x67890");
          con.Open();
          SqlCommand cmd = new SqlCommand("insert into myTable(enrolmentno,aadhaarno,name,fname,address,dob,gender,picimage)values(@a,@b,@c,@d,@e,@f,@g,@h)", con);
          cmd.Parameters.AddWithValue("@a", enrolmentno_txt.Text);
          cmd.Parameters.AddWithValue("@b", aadhaarno_txt.Text);
          cmd.Parameters.AddWithValue("@c", name_txt.Text);
          cmd.Parameters.AddWithValue("@d", fname_txt.Text);
          cmd.Parameters.AddWithValue("@e", address_txt.Text);
          cmd.Parameters.AddWithValue("@f", dateTimePicker1.Text);
          cmd.Parameters.AddWithValue("@g", gender);
          cmd.Parameters.AddWithValue("@h", img_arr1);
          int result = cmd.ExecuteNonQuery();
          if (result > 0)
            MessageBox.Show("Data inserted successfully");
          else
            MessageBox.Show("Data is not inserted in database");
          con.Close();                
            }
            catch(Exception es){
            MessageBox.Show(es.Message);
            }
}

        }

private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            this.Activate();
            string[] files = openFileDialog1.FileNames;

            try
            {
                foreach (string file in files)
                {
                    FileInfo fileInfo = new FileInfo(file);
                    FileStream fileStream = fileInfo.OpenRead();
                    pictureBox2.Image = Image.FromStream(fileStream);
                    Application.DoEvents();
                    fileStream.Close();

                }
            }
                //es
            catch (Exception)
            {
                MessageBox.Show("please select only image files.");
            }
        }

Upvotes: 0

Views: 2713

Answers (1)

Spock
Spock

Reputation: 4910

Are you sure the the image is valid? What line are you getting the error on? Like the error indicates, it's a GDI error, and not a SQL Error.

You can remove the need for GDI by replacing the following lines of code

MemoryStream ms1 = new MemoryStream();
pictureBox2.Image.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] img_arr1 = new byte[ms1.Length];
ms1.Read(img_arr1, 0, img_arr1.Length);

with this

byte[] img_arr1 = System.IO.File.ReadAllBytes(fileName);

where fileName is the file that was selected

Upvotes: 1

Related Questions