2015-07-20 61 views
2

我用foreach循环读取多个图像文件,但我只能流第一个选定的文件。当我尝试保存多个不同的图像时,输出就像第一个选定图像的精确副本,而不是剩下不同的图像。从OpenFileDialog处理多个选定的文件

private void button1_Click(object sender, EventArgs e) 
     { 
      SqlConnection con = new SqlConnection(DBHandler.GetConnectionString()); 
     try 
     { 
      OpenFileDialog fop = new OpenFileDialog(); 
      fop.Multiselect = true; 

      fop.InitialDirectory = "C:\\"; 
      fop.Filter = "JPG,JPEG|*.jpg|PNG|*png"; 
      if (fop.ShowDialog() == DialogResult.OK) 
      { 

       foreach (String files in fop.FileNames) 
       { 
        FileStream FS = new FileStream(@fop.FileName, FileMode.Open, FileAccess.Read); 
        byte[] img = new byte[FS.Length]; 
        FS.Read(img, 0, Convert.ToInt32(FS.Length)); 

        if (con.State == ConnectionState.Closed) 
         con.Open(); 
        SqlCommand cmd = new SqlCommand("SaveImage", con); 
        cmd.CommandType = CommandType.StoredProcedure; 
        cmd.Parameters.Add("@img", SqlDbType.Image).Value = img; 
        cmd.ExecuteNonQuery(); 

       } 

       MessageBox.Show("Image has been saved successfully!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      } 

我想查看同一表单上的所有图像。

我的期望:A-B-C-d(每个字母代表不同检索到的图像 “A” 是从对话框第一选择的文件)

实际输出:A-A-A-A。为什么会发生?

+3

你的代码的一些建议。你的SqlConnection应该在using()中。我建议在FileStream中完成相同的操作,或者至少在完成时完成FS.Close()。您不希望不必要地分配资源。 – dman2306

回答

4

你在循环中,你使用fop.FileName,返回第一个选定的文件:

这个属性只能是一个选定的文件的名称。如果要在多选对话框中返回包含所有选定文件名称的数组,请使用FileNames。

foreach (String files in fop.FileNames) 
{ 
    FileStream FS = new FileStream(@fop.FileName, FileMode.Open, FileAccess.Read); 

    // ... 
} 

更改它,而不是使用迭代变量filename

foreach (String filename in fop.FileNames) 
{ 
    FileStream FS = new FileStream(filename, FileMode.Open, FileAccess.Read); 

    // ... 
} 

相关:OpenFileDialog reads only the first file

+1

哎呀,我刚刚意识到我错误输入了您的解决方案。没有错误对于不便感到抱歉:) +谢谢您的帮助。试图使用对话框本身放置在循环中。它似乎是完全愚蠢的:) – SophisticatedUndoing

-1

你的代码应该是这样的

private void button1_Click(object sender, EventArgs e) 
     { 
      SqlConnection con = new SqlConnection(DBHandler.GetConnectionString()); 
     try 
     { 
      OpenFileDialog fop = new OpenFileDialog(); 
      fop.Multiselect = true; 

      fop.InitialDirectory = @"C:\"; 
      fop.Filter = "JPG,JPEG|*.jpg|PNG|*png"; 
      if (fop.ShowDialog() == DialogResult.OK) 
      { 

       foreach (String files in fop.FileNames) 
       { 
        FileStream FS = new FileStream(@files, FileMode.Open, FileAccess.Read); 
        byte[] img = new byte[FS.Length]; 
        FS.Read(img, 0, Convert.ToInt32(FS.Length)); 

        if (con.State == ConnectionState.Closed) 
         con.Open(); 
        SqlCommand cmd = new SqlCommand("SaveImage", con); 
        cmd.CommandType = CommandType.StoredProcedure; 
        cmd.Parameters.Add("@img", SqlDbType.Image).Value = img; 
        cmd.ExecuteNonQuery(); 

       } 

       MessageBox.Show("Image has been saved successfully!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      } 
+0

抱歉的紧迫感..可以分享错误 – shreesha

+0

通过倾销“固定”的代码,你不帮助任何人学习任何东西。解释你看到了什么问题,是什么原因造成的,以及你如何修复它。在发布之前,请尝试单击“新答案”栏以查看您是否未发布重复内容。 – CodeCaster

+1

肯定我会在未来照顾。感谢@CodeCaster。 – shreesha