我正在创建一个 Label
,有时我会使用 .DrawToBitmap()
。我不知道为什么,但在运行程序一段时间后(经常调用 .DrawToBitmap()
),我会遇到异常:
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)
我无法频繁调用此函数。如果我尝试采取彻底的方法:
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);
}
}
我在5-6秒钟(1000-2000次调用)后收到了异常。问题在哪里?如何避免出现这种情况?
编辑:感谢大家提出的关于Dispose()
的想法 - 如果我在label
上使用,一切都可以完美地运作。 即使我不在Bitmap上使用它也没问题。两个答案都很好,我只能接受其中一个:(