2017-10-25 8 views
0

여러 이미지 파일을 찾아 다른 폴더로 복사하려고합니다. 그래서, 내가 flowlayoutpanel에서 대상 폴더로 모든 이미지를 복사하는 버튼을 만든 후 flowlayoutpanel에서 먼저 storeit 이미지를 탐색하면됩니다. 이제 파일 이름이 다른 경우에도 다른 폴더로 복사 할 때 문제가 있습니다. 이 질문을 해결하기위한 제안.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); 
        } 

       }); 

      } 
    } 

*이 내가 이미지 파일을 검색하고

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 범인 추측 FlowLayout의에 표시하는 방법 코드입니다. Parallel.foreach를 사용할 때 여러 개의 스레드를 사용하고 있습니다. 즉, 동일한 작업을 수행하는 두 개의 스레드가 있습니다.

간단한 foreach 루프를 사용해 보시기 바랍니다.

+0

감사합니다, 나는 이것을 위해 간단한 foreach를 사용했지만 동일한 문제가 발생했습니다. – tang

+0

좋아요, foreach 루프를 병렬로 바꾸는 것이 쉽지만 내 두 목록을 결합하는 옵션을 사용합니다. 제안 해 주셔서 감사합니다. – tang