c#将文件复制到文件夹时,图像文件重复

时间:2017-10-25 02:14:16

标签: c# image foreach flowlayoutpanel

我正在尝试浏览多个图像文件并将它们复制到另一个文件夹。所以,我做的是当我首先在flowlayoutpanel中浏览图像时,然后创建一个按钮将所有图像从flowlayutpanel复制到目标文件夹。现在,当我复制到另一个文件夹时,即使它有不同的文件名,我也有问题。任何解决这个问题的建议。

*这是将文件复制到文件夹的代码。

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

                });

            }
    }

*这是我浏览图像文件并在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 个答案:

答案 0 :(得分:1)

我想parallel.foreach是罪魁祸首。使用Parallel.foreach时使用多个线程 换句话说,你有两个线程做同样的事情。

我建议您尝试使用简单的foreach循环。