Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 591765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:40:08+00:00 2026-05-13T15:40:08+00:00

Here’s my Picture.cs class: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO;

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:40:08+00:00Added an answer on May 13, 2026 at 3:40 pm

    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:

    1133: Total Size = 2.842,11 MB
    1134: Total Size = 2.844,88 MB
    1135: Total Size = 2.847,56 MB
    1136: Total Size = 2.850,21 MB
    1137: Total Size = 2.853,09 MB
    1138: Total Size = 2.855,86 MB
    1139: Total Size = 2.858,59 MB
    1140: Total Size = 2.861,26 MB
    1141: Total Size = 2.863,65 MB
    1142: Total Size = 2.866,15 MB
    1143: Total Size = 2.868,52 MB
    1144: Total Size = 2.870,93 MB
    1145: Total Size = 2.873,64 MB
    1146: Total Size = 2.876,15 MB
    1147: Total Size = 2.878,84 MB
    1148: Total Size = 2.881,92 MB
    1149: Total Size = 2.885,02 MB
    1150: Total Size = 2.887,78 MB
    1151: Total Size = 2.890,57 MB
    1152: Total Size = 2.893,55 MB
    1153: Total Size = 2.896,32 MB
    1154: Total Size = 2.898,92 MB
    1155: Total Size = 2.901,48 MB
    1156: Total Size = 2.904,02 MB
    

    Sourcecode:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Drawing;
    
    namespace SharpLibrary_MediaManager
    {
        public abstract class BaseFile
        {
            public string Name { get; set; }
            public string FileType { get; set; }
            public long Size { get; set; }
            public DateTime CreationDate { get; set; }
            public DateTime ModificationDate { get; set; }
    
            public abstract void getFileInformation(string filePath);
    
        }
    
    
        public class Picture : BaseFile
        {
            public int Height { get; set; }
            public int Width { get; set; }
            public Image Thumbnail { get; set; }
    
            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;
                        Thumbnail = image.GetThumbnailImage(40, 40, null, new IntPtr());
                    }
                }
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                string folderPath = @"C:\Users\arthur\Pictures";
    
                DirectoryInfo folder = new DirectoryInfo(folderPath);
                List<Picture> lol = new List<Picture>();
                double totalFileSize = 0;
                int counter = 0;
                foreach (FileInfo x in folder.GetFiles("*.jpg", SearchOption.AllDirectories))
                {
                    Picture p = new Picture();
                    p.getFileInformation(x.FullName);
                    lol.Add(p);
                    totalFileSize += p.Size;
                    Console.WriteLine("{0}: Total Size = {1:n2} MB", ++counter, totalFileSize / 1048576.0);
                }
    
                foreach (var p in lol)
                {
                    Console.WriteLine("{0}: {1}x{2} px", p.Name, p.Width, p.Height);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 367k
  • Answers 367k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer All three symptoms are bad. They really are a bad… May 14, 2026 at 4:51 pm
  • Editorial Team
    Editorial Team added an answer Nice find, but you are taking advantage of an otherwise… May 14, 2026 at 4:51 pm
  • Editorial Team
    Editorial Team added an answer Here's a helper method that should point you in the… May 14, 2026 at 4:51 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.