views:

132

answers:

1

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);
                        pBox.SizeMode = PictureBoxSizeMode.StretchImage;
                        .
                        .//here i add some eventHandler of picture boxes.

                        this.panel1.Controls.Add(pBox);
                        x += pBox.Width + 4;
                     }
                 } //here w,h,x,y are integers.

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.

+1  A: 

You might consider profiling your method. If you don't have a profiler like ANTS available, you can roll your own:

Stopwatch watch = new Stopwatch();
watch.Start();

//code to profile goes here

watch.Stop();
Console.Writeln("Elapsed time: " + watch.Elapsed.TotalMilliseconds + "ms");

This will help you pinpoint which part of your code is slow.

I can tell you right now that Image.FromFile() is probably the slowest part. What you might consider is loading the images into a List<> first, using a separate thread or background worker. This will let you show a progress bar or hourglass, to let the user know that images are being read from disk.

Once the images are in memory, creating the picture boxes will go a lot faster.

Edit:

You've requested an example showing how to load images into memory first. It's really simple:

// this code should run in its own thread - BackgroundWorker is perfect for this

List<Image> images = new List<Image>();
foreach (string imagePath in paths)
{
   images.Add(Image.FromFile(imagePath));

   // update progress bar here?
}

Now, you have a list of your images which you can use to fill your picture boxes.

Charlie Salts
sir i mentioned in my question the code which is slow.
qulzam
I agree with Charlie. He gave some good examples of how to fix your problem. Load the files into memory first and then add them to the panel. Plus do this operation on a background thread or separate thread so that the app does not freeze while you load 20 - 30 images.
Joshua Hudson
Yes, but which line is slow? As I mentioned in my answer, I think Image.FromFile() is going to be the slowest. Profile your code to verify that it is the slowest.
Charlie Salts
what is mean to load images into memory first. and how it will done. plz explain?
qulzam