Click here to Skip to main content
15,904,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C# 2008

I am showing multiple images files in flowlayout panel and now I want to save these files in database. How can I select names of these files?



C#
public frmMultiSelectFile()
        {
            InitializeComponent();
        InitializeOpenFileDialog();
        }

private void InitializeOpenFileDialog()
{
    // Set the file dialog to filter for graphics files.
    this.openFileDialog1.Filter =
        "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" +
        "All files (*.*)|*.*";
    // Allow the user to select multiple images.
    this.openFileDialog1.Multiselect = true;
    this.openFileDialog1.Title = "My Image Browser";
   
}

private void butSelect_Click(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
                    {
                        PictureBox pb = new PictureBox();
                        Image loadedImage = Image.FromFile(file);
                        pb.Height = 100;
                        pb.Width = 115;
                        pb.Image = loadedImage;
                        pb.SizeMode=
                            PictureBoxSizeMode.StretchImage;

                        flowLayoutPanel1.Controls.Add(pb);
   
                    }
                   
                    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);
                    }
                }

            }
        }
Posted
Updated 17-Nov-11 3:26am
v2
Comments
Smithers-Jones 17-Nov-11 9:27am    
Added code-block.

1 solution

You should create a List<string> as a form level variable. Then in your foreach loop where you add the pictures to the panel save the path of each one as a string, and add it to the List. That way you will have a list of all files selected by the user, to use later.

Hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900