开发者

Winforms Drawing - Parameter is not valid on system resume

I have an odd problem when my app is running and the system resumes from hibernation (in Windows 7).

I am calling the Graphics.DrawString method and this works fine most of the time. Except when the program is running and I hibernate the system. Then when I resume, the DrawString method throws an ArgumentException (Parameter is not valid) and gives me a red cross where my drawing should be.

What is going wrong here? Catching the exception avoids the red cross but when we get into this state there is no way back and the exception will keep being thrown until the program is restart开发者_开发技巧ed.

Thanks for any help, Alan

Edit: Here is the code that is failing:

protected override void OnPaint(PaintEventArgs e)
{
    // Drawing logic succeeds until this point

    e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), new PointF(x, y));
}

This is actually a subclass of ToolStripLabel.


Many system resources become invalid after a sleep or hibernation. So your Font is probably invalid as that is a common cause of such issues. Also I note that you are not calling Dispose on the SolidBrush which you should do that to prevent resource leeks. Like this...

using(SolidBrush drawBrush = new SolidBrush(ForeColor))
    e.Graphics.DrawString(Text, Font, drawBrush, new PointF(x, y));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜