Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting an 'out of memory' exception in this relatively simple program
    text
    copied!<p>Here's my Picture.cs class:</p> <pre><code>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; } /// &lt;summary&gt; /// Sets file information of an image from a given image in the file path. /// &lt;/summary&gt; /// &lt;param name="filePath"&gt;File path of the image.&lt;/param&gt; 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; } } } </code></pre> <p>And here, I'm using that class to pull information from every file in a given folder:</p> <pre><code>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&lt;Picture&gt; lol = new List&lt;Picture&gt;(); foreach (FileInfo x in folder.GetFiles()) { Picture picture = new Picture(); picture.getFileInformation(x.FullName); lol.Add(picture); } MessageBox.Show(lol[0].Name); } } } </code></pre> <p>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.</p> <p>Any help guys? :)</p> <p><strong>Edit:</strong> 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.</p> <p><strong>Edit 2:</strong> 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'.</p> <p>Can someone recommend a different approach? :)</p> <p><strong>Edit 3:</strong> 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?</p> <pre><code>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; } } } </code></pre> <p>Also, should I open a new question now that this one has grown a bit?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload