user1853454
user1853454

Reputation: 141

'System.OutOfMemoryException' occurred in System.Drawing.dll

Can anyone tell me what is wrong with the below code i am having no joy figuring this out. I am disposing and have in a using block but keep getting this exception

An unhandled exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll

Additional information: Out of memory.

foreach (FileInfo file in files)
{
    Regex re = new Regex("original", RegexOptions.IgnoreCase);
    if (re.IsMatch(file.FullName)) continue;

    using (System.Drawing.Image img = System.Drawing.Image.FromFile(file.FullName))
    {
        if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
        {
            filesJpeg.Add(file);
            img.Dispose();
        }
        else
        {
            img.Dispose();
            File.Delete(file.FullName);
        }
 
        img.Dispose();
    }
}

Upvotes: 0

Views: 2972

Answers (1)

Jcl
Jcl

Reputation: 28272

The OutOfMemoryException when opening images might not necessarily mean it's out of memory. It may mean the image format is unknown, or not supported by GDI+ .

As per the documentation (on http://msdn.microsoft.com/en-us/library/stf701f5.aspx):

Exception

OutOfMemoryException

Condition

The file does not have a valid image format.

-or-

GDI+ does not support the pixel format of the file.

I'd check the file where it's throwing the exception and see if it's in a strange format

Upvotes: 1

Related Questions