C# GDI+中的一般错误;清理时
我从API调用我的项目时遇到了这个异常 当我们更改某个值时,从事件调用抛出消息的方法将重新绘制背景并重新绘制字符串。在正常使用应用程序时,这种情况永远不会发生,但当我更改API中的值时,有时会抛出这种情况C# GDI+中的一般错误;清理时,c#,api,gdi+,C#,Api,Gdi+,我从API调用我的项目时遇到了这个异常 当我们更改某个值时,从事件调用抛出消息的方法将重新绘制背景并重新绘制字符串。在正常使用应用程序时,这种情况永远不会发生,但当我更改API中的值时,有时会抛出这种情况 下面是我的代码的样子 public class Drawable : UserControl { private Graphics g; public void OnPaint(PaintEventArgs e) { base.OnPaint(e);
下面是我的代码的样子
public class Drawable : UserControl
{
private Graphics g;
public void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
DrawLabel();
}
private void DrawLabel()
{
if ((TopLabels != null) || (LeftLabels != null))
{
g.Clear( BackColor ); //tje error is in here.
g = CreateGraphics();
/* another things to do*/
}
}
}
您需要将图形从OnPaint()传递到DrawLabel()方法 更改:
DrawLabel();
致:
和变化:
private void DrawLabel()
致:
…并删除CreateGraphics()调用。为什么要删除create graphics?我的意思是,绘图标签不仅从onPaint调用,而且调用error的是onPaint方法。你能给我一个解释吗?因为CreateGraphics()返回一个临时曲面,当控件刷新时该曲面将被擦除。这几乎从来都不是正确的方法。。。
DrawLabel(e.Graphics);
private void DrawLabel()
private void DrawLabel(Graphics g)