Search code examples
c#performanceflowlayoutpanel

Load images on demand into FlowLayoutPanel?


i want to populate a flowlayoutpanel with many images (~400pc., 500x500px). I loop through a string list with the filenames and add this pics to the flp. But above ~200 images the panel doesn't show more images.

I think it's a perfomance problem and i want to ask, wether it is possible to load the images "on demand"?

Like scrolling through the panel and only load the "visible" image?

For panel filling i use this:

public void PanelFill(List<string> filenames)
{
    try
    {
        int temp = 0;
        foreach (string filename in filenames)
        {
            GC.Collect();
            PictureBox pic = new PictureBox
            {
                ClientSize = new Size(int_thumbWidth, int_thumbHeight),
                //Image = new Bitmap(filename),
                Tag = filename,
                BorderStyle = BorderStyle.FixedSingle
            };
            if ((pic.Image.Width > int_thumbWidth) ||
                (pic.Image.Height > int_thumbHeight))
            {
                pic.SizeMode = PictureBoxSizeMode.Zoom;
            }
            else
            {
                pic.SizeMode = PictureBoxSizeMode.CenterImage;
            }
            flowLayoutPanel_esww_bildvergleich.Invoke(new Action(() => { pic.Parent = flowLayoutPanel_esww_bildvergleich; }));
            pic.Click += PictureBox_Click;
            pic.DoubleClick += PictureBox_DoubleClick;
            pic.MouseUp += PictureBox_MouseUp;
            temp++;
        }
    }
    catch (OutOfMemoryException)
    {
        MessageBox.Show("Es wurden zu viele Fotos gefunden. Der Arbeitsspeicher ist nicht ausreichend. Bitte passen Sie Ihre Filter an.", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
        str_aufnahmeTyp.Clear();
    }
    catch (Exception ex)
    {
        Fehlerbehandlung.Fehler(-1, ex.Message);
    }
}

Solution

  • A workaround for me is to add a Scroll/MouseEnter- Eventhandler for catching the scrolling through the panel. In the handler i use .PerformLayout()

     this.flowLayoutPanel_esww_bildvergleich.MouseEnter += new System.EventHandler(this.flowLayoutPanel_esww_bildvergleich_MouseEnter);
    

    and...

            private void flowLayoutPanel_esww_bildvergleich_MouseEnter(object sender, EventArgs e)
        {
            flowLayoutPanel_esww_bildvergleich.Focus();
            flowLayoutPanel_esww_bildvergleich.PerformLayout();
        }