Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone!

I have a list to save .jpg files path

private List<string> images = new List<string>();

I want to fill it with pictures. For example, user selected a folder
with FolderBrowserDialog

  if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  {
          // --- clear previous results ---
         this.images.Clear();
      
      { // I intentionally put brackets here to create a scope ---
         FileInfo[] files = 
(new DirectoryInfo(folderBrowserDialog1.SelectedPath)).GetFiles("*.jpg");
         
         foreach(FileInfo pic in files)
             this.images.Add(pic.FullName);

       } 
     
   using(FileStream fs = new FileStream(images[0], FileMode.Open))
   {
       this.pictureBox.Image = Image.FromStream(fs);
    }

  }


I have two questions
First, if I created that "scope" and i put FileInfo references there
so after the closing bracket they would be out of scope and thus
they will be popped out of stack....Right? Will it save memory that
my application uses? Or will it simply destroy references and will
not free up heap memory where FileInfo references point???

Second, is it effective to open images in Stream like I did??
I used to create a Bitmap object and then I loaded it onto PictureBox
and it was slower (even without deep examing)...

Is there any another way to optimize image loading like "stream cache" or something??
Please help
Posted
Updated 30-Apr-10 7:24am
v2

Hi Nick,

1.
you don't need the FileInfo[] array at all, as all you use is the names anyway. Have a look at Directory.GetFiles(); that one returns an array of strings, much cheaper, much faster.

2.
assuming images is a List<string> or something similar, you don't need the foreach loop, just try images.AddRange()

3.
I don't think the way to load the image into memory and into the PictureBox will matter much. The simplest probably is pictureBox.ImageLocation=images[0];

4.
What will affect performance is you disposing of images you no longer need, so if you plan on cycling them towards your PictureBox, you may want to do it like this:
foreach(string s in images) {
    Bitmap bm=new Bitmap(s);
    pictureBox.Image=bm;
    // insert something that takes a while so you can see the image...
    pictureBox.Image=null;
    bm.Dispose();
}


:)
 
Share this answer
 
Nick Reshetinsky wrote:
f I created that "scope" and i put FileInfo references there
so after the closing bracket they would be out of scope and thus
they will be popped out of stack....Right? Will it save memory that
my application uses? Or will it simply destroy references and will
not free up heap memory where FileInfo references point?


Yes and no. When the "files" array goes out of scope at the end of the block, all the elements of the array (all the FileInfo variables) are available for garbage collection, because nothing else refers to them. They won't however be deleted until the GC actually gets round to it - which may be immediately, or may be next year. It really doesn't matter!

Without actually measuring it (and I don't have time at the moment) I would be suprised if
this.pictureBox.Image = Image.FromStream(fs);
was significantly slower than
this.pictureBox.Image = new Bitmap(pathNameString);
as I would expect the Bitmap constructor to use a stream anyway...
Did you try
this.pictureBox.Image = Image.FromFile(pathNameString);
and if so did that make any difference?
 
Share this answer
 

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



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