DrawToBitmap - System.ArgumentException:参数无效 [英] DrawToBitmap - System.ArgumentException: Parameter is not valid

查看:3711
本文介绍了DrawToBitmap - System.ArgumentException:参数无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林创建一个标签有时即时通讯使用 .DrawToBitmap()。我不知道为什么,但是我跑我的计划了一段时间后(并呼吁 .DrawToBitmap()常)我得到异常:

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);
  }
}

我后5-6秒(1000-2000长途)的异常了。有什么问题?如何避免这种情况?

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

编辑:谢谢你们的想法与的Dispose() - 不知何故一切工作完美,如果我用它标签。即使我不使用它的位图的罚款。这两个问题的答案都是伟大的,我只能接受1人:(

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 :(

推荐答案

因此​​,该错误消息来自于GDI +深跌,可能出现的很多的原因。我看到一个明显的问题,您的code这是一个候选人但是:

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);

字体对象实现的IDisposable ,你在一个紧密的循环创造了很多他们从不叫的Dispose()。这同样适用于位图本身。我敢打赌,GDI是耗尽资源。

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.

这是很难让你的code意义,因为它的立场。它本质上也绝对没有什么,但创建吨字体的位图的对象,所以我不能甚至建议包每个使用语句在的声明。除此之外,当你创建吨接二连三GDI对象,而不处置它们,你最终会碰到这个问题。

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.

如果你需要这些对象是有效的一段时间,那么你需要确保你调用的Dispose()对他们以后释放本地资源,及时的方式为可能的(终结会为你做这个,但最好不要等待它)。如果它们是本地对象,然后将它们包装在一个使用语句,这样的Dispose()将被称为块退出时:

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

这篇关于DrawToBitmap - System.ArgumentException:参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆