【问题标题】:How to save images in sql server using winform?如何使用winform将图像保存在sql server中?
【发布时间】:2015-03-09 10:34:22
【问题描述】:

我正在尝试将 bmp、jpg、gif 和 png 等图像保存到 sql server 数据库中。但无法将所有格式保存到数据库中。只有 png 图像被保存到数据库中。如果尝试保存 jpeg、.bmp 和 .gif 图像,则会显示错误“GDI+ 中发生一般错误”。有什么问题?

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.");
            }
        }

【问题讨论】:

  • picbox 中的图像是从哪里来的(它是如何到达那里的)...确实,那里有图像吗?
  • 从 openFileDialog1 加载图像
  • 您是否在 Image.Save 行上遇到异常?
  • @MarkPM 可以使用 Image.Save 行,但未执行到下一行。立即转向异常。
  • 旁白:一个很好的建议是非常确定您首先需要存储图像。不是好习惯,IMO。去过也做过。尽可能存储文件,并仅链接到数据库中的这些文件。

标签: c# sql-server winforms


【解决方案1】:

您确定图片有效吗? 你在哪一行得到错误? 如错误所示,这是 GDI 错误,而不是 SQL 错误。

您可以通过替换以下代码行来消除对 GDI 的需求

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);

有了这个

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

fileName 是被选中的文件

【讨论】:

  • 我没有使用单个图像来获取错误。它只接受 png 图像。如何在代码中提及fileName?
  • fileName 是在 openFileDialog1 中选择的文件的路径
  • 你必须删除上面的 4 行代码......它们不能执行。 File.ReadAllBytes 不可能出现 GDI 错误
  • 什么问题?请用您当前的代码和错误更新您的问题
  • File.ReadAllBytes 不会导致 GDI 错误。我认为您没有取消注释上面的 4 行代码。或者你的错误是由其他原因引起的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多