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

  • SEARCH
  • Home
  • 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 6812077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:24:19+00:00 2026-05-26T20:24:19+00:00

i use foreach to read all the image from a folder string[] filePaths =

  • 0

i use foreach to read all the image from a folder

        string[] filePaths = Directory.GetFiles(Workspace.InputFolder, "*.*");
        foreach (string imageFile in filePaths)
        {
            // Some Process here, the output are correct, just after output 
               the error happen
        }

But it come out error

System.OutOfMemoryException was unhandled
  Message=Out of memory.
  Source=System.Drawing 

Is the problem cause by the foreach loop keep looping after the process finish?
What should i do for releasing the memory?
Thanks.

  • 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-26T20:24:20+00:00Added an answer on May 26, 2026 at 8:24 pm

    Given your exception, it looks like you’re working with objects in the System.Drawing namespace.

    If you are opening and manipulating an image in your foreach loop, for example, make sure that you call Dispose() to release the images resources as soon as you’re done with them. You can, alternatively, wrap this in a using statement, ie:

        foreach (string imageFile in filePaths)
        {
            using (var image = Image.FromFile(imageFile)
            {
                // Use the image...
            } // Image will get disposed correctly here, now.
        }
    

    Be aware that it’s not just images that are potentially the problem, but any resource that implements IDisposable. Many of the classes in System.Drawing are disposable – make sure that you either access them as above (via using) or call Dispose() on them when done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do following: Make Oracle stored procedure for read all data from
I use the foreach shortcut all the time. In many cases I do the
I'm trying to use doSMP / foreach to parallelize some code in R. I
All, I am using the Parallels.ForEach as follows private void fillEventDifferencesParallels(IProducerConsumerCollection<IEvent> events, Dictionary<string, IEvent>
I need the reference system32/shell32.dll as I use some shell functions to read out
I'm trying to read all of the attributes in the 'Data' elements from an
I'm trying to read a XML-RSS-Feed from a website. Therefore I use a async
I'd like to use a foreach loop to insert a _POST associative array into
I'm trying to use a foreach loop for an array of objects. Inside of
In PHP I can use a foreach loop such that I have access to

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.