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 9249549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:13:27+00:00 2026-06-18T10:13:27+00:00

I have a method that loops through a data collection, builds bitmaps based on

  • 0

I have a method that loops through a data collection, builds bitmaps based on the data in the collection retrieved from the database, and finally adds the built bitmaps in a collection that I call DoorSchedules.

In my loop if I call .dispose on my graphics I receive an error. This error does not happen when I do not call the .dispose method that belongs to the Graphics object.

When should I call dispose on the graphics object when looping, do I create a new graphics object for each loop?

The code works below, until I un-comment the dc.Dispose.

     DoorSchedules schedules = new DoorSchedules();

for (byte i = 0; i < elevation.Bays.Count; i++)
                {
                    if (elevation.Bays[i].HasDoor.Value)
                    {
                        for (byte ii = 0; ii < elevation.Bays[i].Doors.Count; ii++)
                        {

                            door = elevation.Bays[i].Doors[ii];
                            width = getInchPx(door.WidthInches);
                            height = getInchPx(door.HeightInches);

                            Bitmap canvas = new Bitmap((int)width + DOOR_SCHEDULE_WIDTH_ADD,
                                                       (int)height + DOOR_SCHEDULE_HEIGHT_ADD);

                            Graphics dc = Graphics.FromImage(canvas);

                ..work removed for readability for stackoverflow.

                            schedules.Add(new DoorSchedule(canvas));

                            //dc.Dispose();
                        };
                    };
                };
  • 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-06-18T10:13:28+00:00Added an answer on June 18, 2026 at 10:13 am

    You can try enclosing your objects that require disposing in using statements, that way the resources will be released automatically when finished.

    From above link:

    As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.

    See if something like this works for you, if it doesn’t please supply the error that you are receiving.

    using (Bitmap canvas = new Bitmap((int)width + DOOR_SCHEDULE_WIDTH_ADD,(int)height + DOOR_SCHEDULE_HEIGHT_ADD))
    {
        using( Graphics dc = Graphics.FromImage(canvas))
        {
    
        }
    }
    

    In looking at your previous question it looks like you abandoned the use of using because you ended up disposing your Bitmap, if this is the same code you can try creating a Clone of your Bitmap instead.

    so in your case it would look something like:

    using (Bitmap canvas = new Bitmap((int)width + DOOR_SCHEDULE_WIDTH_ADD,(int)height + DOOR_SCHEDULE_HEIGHT_ADD))
    {
        using( Graphics dc = Graphics.FromImage(canvas))
        {
             dc.DrawEllipse(Pens.Red, 10, 10, 50, 50);
             pictureBox1.Image = (Bitmap)canvas.Clone(); //The PictureBox is just an object to accept the newly created bitmap.
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this code below that loops through a data structure builds up a
I have a ruby method that performs a complex data collection and places the
I have a JUnit 4 test that loops through an array of test data:
I have a method that loops through a list of guids and saves them
For example I have a method that looks like this [self performSelectorOnMainThread:@selector(someMethod) withObject:data waitUntilDone:YES];
I have a collection-based project in .NET 4. What I mean is that, I
I need to write a method that loops through ArrayList<String> pathClientStatic and then copies
I have an app that loops through a fixed width text file, reads each
I'm using BackboneJS, I have a got a collection which gets its data from
I have a method that calls a Proc ad returns the data. But I

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.