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.

Dani AI

Generated

Good direction from — the slowdown is usually two things: synchronous image I/O on the UI thread and creating/laying out a large number of controls at once. A practical pattern that helps a lot is: (1) read files off the UI thread, (2) create small thumbnails (not full-size images) in background work, (3) marshal a quick UI update for each thumbnail (add one PictureBox at a time), and (4) dispose images when no longer needed.

Example: load thumbnails into a List<Image> on a background thread and add PictureBoxes on the UI thread as each thumbnail is ready. This keeps the UI responsive and spreads layout cost over time.

List<Image> thumbs = new List<Image>();

Task.Run(() =>
{
    foreach (var path in files)
    {
        using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
        using (var img = Image.FromStream(fs))
        {
            var thumb = new Bitmap(120, 90);
            using (var g = Graphics.FromImage(thumb))
            {
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(img, 0, 0, thumb.Width, thumb.Height);
            }

            lock (thumbs) thumbs.Add(thumb);

            this.BeginInvoke((Action)(() =>
            {
                var pb = new PictureBox { Image = thumb, SizeMode = PictureBoxSizeMode.Zoom, Size = thumb.Size };
                panel1.Controls.Add(pb);
            }));
        }
    }
});

A few more practical tips:

  • Use a FlowLayoutPanel (or a subclass with DoubleBuffered enabled) to avoid repeated manual positioning and reduce flicker. Example subclass:
public class DoubleBufferedFlowLayoutPanel : FlowLayoutPanel
{
    public DoubleBufferedFlowLayoutPanel() { DoubleBuffered = true; ResizeRedraw = true; }
}
  • Dispose images when removing controls: call pb.Image.Dispose() before removing the PictureBox.
  • For very large sets, prefer a virtualized UI (ListView in virtual mode + ImageList, or custom owner-draw) and persistent thumbnail caching on disk so thumbnails are reused instead of regenerated.

This approach builds on ’s suggestion and gives a concrete way for to populate a List<Image> and update the panel without blocking the UI.

Recommended Answers

All 4 Replies

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.