2017-10-25 1 views
0

複数の画像ファイルを参照して別のフォルダにコピーしようとしています。それで、私が行うのは、flowlayoutpanelのイメージをブラウズするときです。その後、flowlayutpanelからデスティネーションフォルダにすべてのイメージをコピーするボタンを作成します。今、別のファイル名を持っていても別のフォルダにコピーするときに問題があります。この質問を解決するための提案。c#ファイルをフォルダにコピーすると画像ファイルが複製される

*これは、ファイルをフォルダにコピーするコードです。

private void button2_Click(object sender, EventArgs e) 
    { 


     foreach (TextBox tb1 in TextBoxes1) 
     { 

      MessageBox.Show(tb1.Text); 

       String[] files = openFileDialog1.FileNames; 
       String newDir = @"C:\Users\Public\Pictures\Sample Pictures\Modified"; 
       System.IO.Directory.CreateDirectory(newDir); 


       Parallel.ForEach(files, (textboxes) => 
       { 
        foreach (TextBox tb in TextBoxes) 
        { 

         String filename = System.IO.Path.GetFileName(tb.Text); 
         var bitmap = new Bitmap(textboxes); 
         String text = Path.Combine(newDir, filename); 
         string toDisplay = string.Join(Environment.NewLine, files); 
         MessageBox.Show(toDisplay); 

         bitmap.Save(text); 
        } 

       }); 

      } 
    } 

*これは私がparallel.foreachが犯人だと思いFlowLayoutの

private void button1_Click_2(object sender, EventArgs e) 
    { 
     DialogResult dr = this.openFileDialog1.ShowDialog(); 

     if (dr == System.Windows.Forms.DialogResult.OK) 
     { 

      // Read the files 
      foreach (String file in openFileDialog1.FileNames) 
      { 
       // Create a PictureBox. 
       try 
       { 
        Panel panel = new Panel(); 
        PictureBox pb = new PictureBox(); 
        TextBox tb = new TextBox(); 
        TextBox tb1 = new TextBox(); 
        Image loadedImage = Image.FromFile(file); 
        pb.Height = 200; 
        pb.Width = 200; 
        pb.Image = loadedImage; 
        pb.SizeMode = PictureBoxSizeMode.StretchImage; 
        tb.Text = Path.GetFileName(openFileDialog1.FileName); 
        tb1.Text = openFileDialog1.FileName; 
        //panel.Controls.Add(pb); 
        panel.Controls.Add(tb); 
        panel.Controls.Add(tb1); 
        panel.Height = 200; 
        panel.Width = 200; 
        flowLayoutPanel1.Controls.Add(panel); 
        TextBoxes.Add(tb); 
        TextBoxes1.Add(tb1); 

       } 
       catch (SecurityException ex) 
       { 
        // The user lacks appropriate permissions to read files, discover paths, etc. 
        MessageBox.Show("Security error. Please contact your administrator for details.\n\n" + 
         "Error message: " + ex.Message + "\n\n" + 
         "Details (send to Support):\n\n" + ex.StackTrace 
        ); 
       } 
       catch (Exception ex) 
       { 
        // Could not load the image - probably related to Windows file system permissions. 
        MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\\')) 
         + ". You may not have permission to read the file, or " + 
         "it may be corrupt.\n\nReported error: " + ex.Message); 
       } 


      } 
     } 

答えて

1

の画像ファイルやショーを閲覧する方法のコードです。 Parallel.foreachを使用するときに複数のスレッドを使用しています つまり、同じことをする2つのスレッドがあります。

単純なforeachループを試してみることをお勧めします。

+0

ありがとう、私はこれに簡単なforeachを使用しましたが、同じ問題が発生します – tang

+0

私の並列foreachループは単純なforeachループに変更されますが、私は2つのリストを組み合わせるために "ZIP" – tang

関連する問題