Im creates Label, and sometimes Im uses .DrawToBitmap(). I don’t know why, but after I run my program for a while (and often calling .DrawToBitmap()), I get an 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 can not often call this function. If I strongly tried 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 received an exception after 5-6 seconds (1000-2000 calls). What is the problem? How to avoid this?
Edit: Thank you guys for the idea with Dispose()- somehow everything works fine if I use it on Label. Even if I do not use it in Bitmap, that’s fine. Both answers are great, I can only accept 1 of them :(
source
share