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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:04:54+00:00 2026-06-10T00:04:54+00:00

Im creating a Label and sometimes Im using .DrawToBitmap() . I dont know why,

  • 0

Im creating a Label and sometimes Im using .DrawToBitmap(). I dont know why, but after Im running my program for a while (and calling .DrawToBitmap() often) I get the exception:

System.ArgumentException: Parameter is not valid.
   at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
   at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)

Somehow I cannot call this function that often. If I would radically try this:

while(true)
{

  System.Windows.Forms.Label label = new Label();

  label.Font = new Font("Arial", 20);
  label.Text = "test";

  try
  {
    Bitmap image = new Bitmap(300, 500);
    label.DrawToBitmap(image, label.ClientRectangle);
  }
  catch (Exception e)
  {
    Console.WriteLine(e);
  }
}

I got after 5-6 secs (1000-2000 calls) the exception. What is the problem? How to avoid this?

Edit: Thank you guys for the idea with Dispose() – somehow everything is working perfect if I use it on label. Even if I dont use it on Bitmap its fine. Both of the answers are great, I can only accept 1 of them 🙁

  • 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-10T00:04:55+00:00Added an answer on June 10, 2026 at 12:04 am

    So, that error message comes from deep down in GDI+ and may appear for a lot of reasons. I see one glaring problem with your code that is a candidate however:

     label.Font = new Font("Arial", 20);
    

    Font objects implement IDisposable and you are creating a lot of them in a tight loop and never calling Dispose(). Same goes for the Bitmap itself. I would bet that GDI is running out of resources.

    It’s hard to make sense of your code as it stands. It essentially does absolutely nothing but create a ton of Font and Bitmap objects, so I can’t even suggest to wrap each of those declarations in a using statement. That aside, when you create a ton of GDI objects in quick succession without disposing of them you will eventually run into this problem.

    If you need these objects to be valid for some time then you need to make sure you call Dispose() on them later to release native resources in as timely a manner as possible (the finalizer will do this for you, but it’s best not to wait for it to). If they are local objects then wrap them in a using statement so Dispose() will be called when the block exits:

    using(var b = new Bitmap(w, h))
    {
        // use 'b' for whatever
    } // b.Dispose() is called for you
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I am creating a button in an oval shape. But my button label
I am having dynamically creating remove label. All have same class but different Ids.
Sometimes I get myself using different types of trees in Haskell and I don't
I'm creating a UIToolbar with nothing but a UILabel placed on it. the label
After creating a label and adding to a layer, I need to find out
I'm having a table in which i'm creating a label dynamically. '<td>' + '<label
I'm trying to create a simple two labeled list by creating two label label
I am creating a form with <ol> , <label> and <textbox> in asp.net the
(creating a separate question after comments on this: Javascript redeclared global variable overrides old
I am creating a label control which can be used on Aero Glass and

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.