Here’s my Picture.cs class:
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; }
public Image Thumbnail { 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);
if (fileInformation.Exists)
{
Name = fileInformation.Name;
FileType = fileInformation.Extension;
Size = fileInformation.Length;
CreationDate = fileInformation.CreationTime;
ModificationDate = fileInformation.LastWriteTime;
Height = calculatePictureHeight(filePath);
Width = calculatePictureWidth(filePath);
}
}
public override void getThumbnail(string filePath)
{
Image image = Image.FromFile(filePath);
Thumbnail = image.GetThumbnailImage(40, 40, null, new IntPtr());
}
private int calculatePictureHeight(string filePath)
{
var image = Image.FromFile(filePath);
return image.Height;
}
private int calculatePictureWidth(string filePath)
{
var image = Image.FromFile(filePath);
return image.Width;
}
}
}
And here, I’m using that class to pull information from every file in a given folder:
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\PictureFolder";
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo folder = new DirectoryInfo(folderPath);
List<Picture> lol = new List<Picture>();
foreach (FileInfo x in folder.GetFiles())
{
Picture picture = new Picture();
picture.getFileInformation(x.FullName);
lol.Add(picture);
}
MessageBox.Show(lol[0].Name);
}
}
}
I’m getting an Out Of Memory exception and I don’t really know why. This is the first time I’m doing something like this so I’m pretty new to batch file processing, etc.
Any help guys? 🙂
Edit:
I opened the Task Manager to see memory usage and when I press the Button to run the method I notice my memory usage increases by 100mb~ every second.
Edit 2:
In my folder I have about 103 images, each image being ~100kb.
I need a solution where it doesn’t matter how many images are in a folder. Someone recommended opening an image, doing my magic, then close it. I don’t really understand what he meant by ‘close’.
Can someone recommend a different approach? 🙂
Edit 3:
Still getting the out of memory exception, I’ve changed the code in Picture.cs based on recommendations, but I’m out of ideas. Any help?
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;
}
}
}
Also, should I open a new question now that this one has grown a bit?
You are not calling Dispose on your Image instances. Also create your image once and then extract your data.
See also:
http://msdn.microsoft.com/en-us/library/8th8381z.aspx
EDIT
If copied your code and tested it with my Picture Library. My avg. FileSize is 2-3 MB per File. I’ve executed your program and it did exactly what it should. The GC did exactly what I’ve expected.
Memory of your Program was always about 11-35 MB Private Working set, Commit Size was stable at 43 MB.
I’ve aborted the program after 1156 files with a total picture size of 2.9 GB.
So there must be another reason for you out of memory exception.
Here’s my program output and code:
Sourcecode: