2015-04-28 111 views
-1

我得到JIT编译错误运行此代码FillEllipse函数错误

void draw(PaintEventArgs e) 
{ 
    Graphics gr =this.CreateGraphics(); 
    Pen pen = new Pen(Color.Black, 5); 
    int x = 50; 
    int y = 50; 
    int width = 100; 
    int height = 100; 
    gr.DrawEllipse(pen, x, y, width, height); 
    gr.Dispose(); 
    SolidBrush brush = new SolidBrush(Color.White); 
    gr.FillEllipse(brush, x,y,width,height); 
} 

错误说:系统参数异常:在 FillEllipse函数(刷无效的说法,INT32 X,INT32 Y,INT32宽度,INT 32高度);

+0

你一定要明白,你实际上是在配置'Graphics'对象,然后尝试再次使用它,你呢? –

+0

啊对不起,我在发帖后提到它,但现在我有另一个问题,如何在不同的显示器上使表单大小静态?由不同的维度?对不起,谢谢 –

+0

使用'CreateGraphics'几乎总是一个错误。你的'draw'方法有一个'PaintEventArgs'传入它,我假设你从某种'Paint'事件中获得。您应该使用来自该图形的Graphics实例:'Graphics gr = e.Graphics'。并且不要丢弃它。 –

回答

0

既然你是通过PaintEventArgs e你可以并应该使用它的e.Graphics

由于您没有创建它,请不要处理它!

但那些PensBrushes你创建你应该处置或更好,但创建它们在一个using子句!对于SolidBrush,我们可以使用标准Brush,这是我们不能改变的,也不能处理!

为了确保填充不会覆盖Draw,我已经切换了订单。

所以,试试这个:

void draw(PaintEventArgs e) 
{ 
    Graphics gr = e.Graphics; 
    int x = 50; 
    int y = 50; 
    int width = 100; 
    int height = 100; 
    gr.FillEllipse(Brushes.White, x, y, width, height); 
    using (Pen pen = new Pen(Color.Black, 5)) 
     gr.DrawEllipse(pen, x, y, width, height); 
}