0

I'm loading a bunch of files into a ListView, and I use Image.FromFile() and .Width and .Height to display the the resolution of each file in a designated column. However, whenever I load more than a handful of files, this method starts skipping files. The first, say 50 files are all loaded properly but after that more and more files are skipped.

What could cause this problem, and are any better alternative methods available? This method also slows down the loading process considerably, so hopefully this process can be sped up a little bit.

EDIT: Sorry... Here's the relevant code:

private void AddItem(string file)
{
     FileInfo fi = new FileInfo(file);
     ListViewItem item = new ListViewItem(fi.FullName);

     try
     {
          Image img = Image.FromFile(fi.FullName);
          item.SubItems.Add(string.Format("{0} x {1}", img.Width, img.Height));
     }

     catch { }

     ((ListView)sender).Items.Add(item);

}

The try/catch methods are meant to prevent errors for non-image files. However, they also seem to be 'responsible' for the skipping issue. When I disable them I get an 'out of memory' error, which seems to be the real cause here.

Daan
  • 1,417
  • 5
  • 25
  • 40
  • 2
    you really should `Dispose` `img`. – Daniel A. White Aug 29 '12 at 16:05
  • Ugh... of course. Yes, that solves the memory issue. What about the speed? Is there any way to quicken the loading process? – Daan Aug 29 '12 at 16:11
  • Well, for speed, can you open up the metadata in the image file and parse that rather than decompress the entire image? For example, look at the JFIF and PNG file formats: you might be able to read the information you need without the overhead of decompression. For JPEG, they don't include file size directly; you'd have to look at an Adobe Image Resource Block. – Ted Spence Aug 29 '12 at 16:40
  • Alright, I found this solution: http://stackoverflow.com/a/112711/1091437. It works wonderfully :) Thanks again.; – Daan Aug 30 '12 at 11:00

0 Answers0