Click here to Skip to main content
15,917,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
//--form is having one flowLayoutPanel1 one fileopen dialog box and 1 button

private void Form1_Load(object sender, EventArgs e)
{
    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 selectFilesButton_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 = loadedImage.Height;
                pb.Width = loadedImage.Width;
                pb.Image = loadedImage;
                flowLayoutPanel1.Controls.Add(pb);
            }
            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);
            }
        }
    }
Posted
Updated 13-Nov-11 22:35pm
v2
Comments
[no name] 14-Nov-11 4:22am    
FORMAT your code snippets like everyone here does!
Timberbird 14-Nov-11 4:44am    
What happens when you run this code? You see image for the last file only? If yes, the reason may be that you don't set each new image coordinates so they all stack in one place and overlap
BillWoodruff 14-Nov-11 5:39am    
Since LanFanNinja, in his excellent answer, has told you they can get your code to work: please describe here what version of Visual Studio you are using, and what version (number) of the .NET FrameWork you are compiling against (Client ? Full ?).

If we can compare the versions you and LanFanNinja are using, that might mean something.
LanFanNinja 14-Nov-11 6:59am    
I was using .NET Framework 4 Client Profile (the default in VS 2010 PRO).

Your code works fine for me. Make sure your FlowLayoutPanel is either Anchored or Docked properly, you have AutoSize set to True or your layout panel is large enough to fit all your picture boxes onto it or you will not see them all.
Here are some examples of some things you can try.
C#
//Set anchor to all sides
flowLayoutPanel1.Anchor = 
    AnchorStyles.Bottom | AnchorStyles.Top | 
    AnchorStyles.Left | AnchorStyles.Right;


or

C#
//set dock to entire form
flowLayoutPanel1.Dock = DockStyle.Fill;


or

C#
flowLayoutPanel1.AutoSize = true; //set AutoSize to True
 
Share this answer
 
v3
Comments
BillWoodruff 14-Nov-11 5:36am    
+5 excellent answer ! And, a logical follow-up is to ask both the OP and you to specify what version of .NET FrameWork and/or Visual Studio you are using.
LanFanNinja 14-Nov-11 6:58am    
Thank you! You have a very good point I was using .NET Framework 4 Client Profile (the default in VS 2010 PRO).
akit_kmr 17-Nov-11 2:45am    
thnx flowLayoutPanel1.AutoSize = true; worked
akit_kmr 18-Nov-11 3:15am    
how to add vscroll bar here with panel
VS 2008 C# PROG ON RUNNING SHOWS LAST IMAGE
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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