0

today, I've run into a bit of a problem. The image object you can see below keeps giving me an outofmemory exception as I iterate through the list of files. I'm trying to make a list of resolutions of each image.

I've tried disposing it to remove the object from memory but that doesn't seem to work....

List<string> temp = new List<string>();
Image img;
foreach(string s in fileArray)
            {
                img = Image.FromFile(s);
                temp.Add(img.Width.ToString() + "x" + img.Height.ToString());
                img.Dispose();
            }
JurreT
  • 19
  • 4
  • 1
    The problem is the Format of the Image. There are some question on SO to this problem – Jehof Feb 12 '15 at 13:37
  • 2
    Out of Memory exception is, unfortunately, the "I give up, I don't know what's wrong" exception of choice by the imaging system. It may not (and usually doesn't) have anything to do with memory. – Damien_The_Unbeliever Feb 12 '15 at 13:37
  • possible duplicate of [ImageList / Image OutOfMemoryException](http://stackoverflow.com/questions/882619/imagelist-image-outofmemoryexception) – AFract Feb 12 '15 at 13:37
  • See also http://stackoverflow.com/questions/23801520/getting-outofmemoryexception-when-i-load-a-tga-file-of-1-mb-in-c-sharp/23801619#23801619 – AFract Feb 12 '15 at 13:39
  • strangely enough I found at least once that calling the GC in code ever now and then did help.. – TaW Feb 12 '15 at 13:39
  • 1
    To spell out what Jehof means: put the `Image.FromFile()` into a try-catch clause! – TaW Feb 12 '15 at 13:42
  • Thanks everyone it turned out it had nothing to do with the memory at all. i removed the dispose and garbage collection, and i also found out this error was given by a corrupt file. – JurreT Feb 12 '15 at 14:06

1 Answers1

0

Glad that you got the problem figured out!

FYI: Whenever you are utilizing a type that wraps unmanaged resources, you will want to use a using block. Details: https://msdn.microsoft.com/en-us/library/yh598w02.aspx

Here is your sample re-written with the using (and var instead of explicitly defined types)

var temp = new List<string>();
foreach(var s in fileArray)
  using(var img = Image.FromFile(s))
  {
    temp.Add(img.Width.ToString() + "x" + img.Height.ToString());
  }
Brandon
  • 1,239
  • 9
  • 17