Search code examples
c#imageforeachflowlayoutpanel

c# the image file is duplicated when I copy file to the folder


I am trying to browse multiple image files and copy them to another folder. So, what I do is when I browse the image I storeit in flowlayoutpanel first, after that I create a button to copy all the image from flowlayutpanel to the destination folder. Now,I have a problem when I copy to another folder even if it has a different file name. Any suggestions to solve this question.

*this is the code to copy file to folder.

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

                });

            }
    }

* this is the code how i browse the image file and show in 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);
                }


            }
        }

Solution

  • I guess parallel.foreach is the culprit. You are using multiple threads when using Parallel.foreach In other words, you have two threads doing the same thing.

    I would suggest you to try with a simple foreach loop.