6 Replies - 1077 Views - Last Post: 05 February 2010 - 10:54 AM Rate Topic: -----

#1 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

OutOfMemory Exception in my small app.

Posted 05 February 2010 - 08:07 AM

I have a Picture.cs class that handles all image related information:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

namespace SharpLibrary_MediaManager
{
    public class Picture:BaseFile
    {
        public int Height { get; set; }
        public int Width { get; set; }

        /// <summary>
        /// Sets file information of an image from a given image in the file path.
        /// </summary>
        /// <param name="filePath">File path of the image.</param>
        public override void  getFileInformation(string filePath)
        {
            FileInfo fileInformation = new FileInfo(filePath);
            
            using (var image = Image.FromFile(filePath))
            {
                if (fileInformation.Exists)
                {
                    Name = fileInformation.Name;
                    FileType = fileInformation.Extension;
                    Size = fileInformation.Length;
                    CreationDate = fileInformation.CreationTime;
                    ModificationDate = fileInformation.LastWriteTime;
                    Height = image.Height;
                    Width = image.Width;
                }
            }
        }
    }
}



And here's I use it in my Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SharpLibrary_MediaManager
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string folderPath = @"D:\Images\Family";

        private void button1_Click(object sender, EventArgs e)
        {
            DirectoryInfo folder = new DirectoryInfo(folderPath);
            string test = string.Empty;
            foreach (FileInfo x in folder.GetFiles())
            {
                Picture picture = new Picture();
                picture.getFileInformation(x.FullName);
                test = picture.Height + "x" + picture.Width;
                
            }

            MessageBox.Show(test);
        }
    }
}


Now, the code works 100% how I want it to work so far with folders that have few pictures in it. If I try to run it with a folder that has 100~ images, I get the outofmemory exception.

Can someone point out what I'm doing wrong?

Thanks. :)

Is This A Good Question/Topic? 0
  • +

Replies To: OutOfMemory Exception in my small app.

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: OutOfMemory Exception in my small app.

Posted 05 February 2010 - 08:40 AM

It looks like you are disposing of the Image fine. However, going by the documentation of the Image.FromFile() method, it will throw an "OutOfMemoryException" in these conditions...

Quote

The file does not have a valid image format.
-or-
GDI+ does not support the pixel format of the file.


Are there any files in the directory that are not image files? If so, then you will need to add a search pattern to the "GetFiles" method.

foreach (FileInfo x in folder.GetFiles("*.jpg"))
{
    Picture picture = new Picture();
    picture.getFileInformation(x.FullName);
    test = picture.Height + "x" + picture.Width;
                
}


Was This Post Helpful? 0
  • +
  • -

#3 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: OutOfMemory Exception in my small app.

Posted 05 February 2010 - 09:01 AM

I believe you are trying to create an image from a file that is not a picture and you get an out-of-memory-exception. I tested you code and this exception has been thrown when I tried to create an image from a desktop.ini file (this is a protected operating system file that is hidden by default in your Windows Explorer, but your code still tries to create an image of it).

Hope this helps...
Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: OutOfMemory Exception in my small app.

Posted 05 February 2010 - 09:04 AM

Something wierd happened. I added the search pattern to the GetFiles method exactly as you wrote it, but I think it shouldn't be working.

In my folder I only have .jpeg images and a single .gif image. The search pattern I wrote is for "*.jpg", are jpeg and jpg the same? I'm confused with that.


Quote

Note:
When using the asterisk wildcard character in a searchPattern (for example, "*.txt"), the matching behavior varies depending on the length of the specified file extension. A searchPattern with a file extension of exactly three characters returns files with an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern. A searchPattern with a file extension of one, two, or more than three characters returns only files with extensions of exactly that length that match the file extension specified in the searchPattern. When using the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files in a directory, "file1.txt" and "file1.txtother", a search pattern of "file?.txt" returns only the first file, while a search pattern of "file*.txt" returns both files.

The following list shows the behavior of different lengths for the searchPattern parameter:

"*.abc" returns files having an extension of.abc,.abcd,.abcde,.abcdef, and so on.

"*.abcd" returns only files having an extension of.abcd.

"*.abcde" returns only files having an extension of.abcde.

"*.abcdef" returns only files having an extension of.abcdef.


Assuming they are the same filetype, why would the single .GIF image give my trouble. I can't have a .GIF assigned to the Image variable with Image.FromFile()?

Thanks for the help.

View PostFlashM, on 05 February 2010 - 08:01 AM, said:

I believe you are trying to create an image from a file that is not a picture and you get an out-of-memory-exception. I tested you code and this exception has been thrown when I tried to create an image from a desktop.ini file (this is a protected operating system file that is hidden by default in your Windows Explorer, but your code still tries to create an image of it).

Hope this helps...


Thank you for the heads up. Does the GetFiles() method get every single file in a folder (even hidden files)? If so, I have to create a filter of sorts to only 'Get' certain file types.

This post has been edited by stapia.gutierrez: 05 February 2010 - 09:14 AM

Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: OutOfMemory Exception in my small app.

Posted 05 February 2010 - 09:16 AM

View Poststapia.gutierrez, on 05 February 2010 - 11:04 AM, said:

Something wierd happened. I added the search pattern to the GetFiles method exactly as you wrote it, but I think it shouldn't be working.

In my folder I only have .jpeg images and a single .gif image. The search pattern I wrote is for "*.jpg", are jpeg and jpg the same? I'm confused with that.

Assuming they are the same filetype, why would the single .GIF image give my trouble. I can't have a .GIF assigned to the Image variable with Image.FromFile()?

Thanks for the help.

View PostFlashM, on 05 February 2010 - 08:01 AM, said:

I believe you are trying to create an image from a file that is not a picture and you get an out-of-memory-exception. I tested you code and this exception has been thrown when I tried to create an image from a desktop.ini file (this is a protected operating system file that is hidden by default in your Windows Explorer, but your code still tries to create an image of it).

Hope this helps...


Thank you for the heads up. Does the GetFiles() method get every single file in a folder (even hidden files)? If so, I have to create a filter of sorts to only 'Get' certain file types.


Also, don't forget about the "thumbs.db" file that is created when you view the thumbnails of a folder.

Why don't you put a breakpoint in your Picture class and see what the filename is of the file that crashes?

This post has been edited by eclipsed4utoo: 05 February 2010 - 09:17 AM

Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: OutOfMemory Exception in my small app.

Posted 05 February 2010 - 09:35 AM

If I select "View Hidden Files/Folders" from Windows Explorer, should I be able to see this Thumbs.db file? I can't see it anywhere in my picture folder so that must means it's not there, right?
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: OutOfMemory Exception in my small app.

Posted 05 February 2010 - 10:54 AM

you need to also show system files. the thumbs.db is a system file.

Attached Image
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1