I want to select the pictures (They are select during program is running) and show them on the form. for that i take a panel on the form and populate the panel with pictureboxes.i write the following code for that but it is very time consuming:

if(openDialoge1.ShowDialog() == DialogResult.OK)
                    {
                       string[] fileName = open.FileNames;
                       foreach (string s in fileName)
                       {
                            pBox = new PictureBox();
                            pBox.Size = new System.Drawing.Size(w, h);
                            pBox.Location = new System.Drawing.Point(x, y);
                            pBox.Image = Image.FromFile(s);
                           // .
                            //.//here i add some eventHandler of picture boxes.

                            this.panel1.Controls.Add(pBox);
                            x += pBox.Width + 4;
                         }
                     }

This code work well, but it is very time consuming and take much time to populate the panel with picture boxes. for example when i slect the 20,30 pictures, it take much time.
Is there any way that reduce the time to populate the panel with pictureboxes.

Thanks in advance.

I think Image.FromFile() will affect the performance. Load the images into a List<> first, using a separate thread or background worker.

I have no idea that how can i use the seperate thread. can u explain with coding

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.