【问题标题】:FillEllipse Error填充椭圆错误
【发布时间】:2015-04-28 14:02:57
【问题描述】:

运行此代码时遇到JIT 编译错误

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(Brush, int32 x,int32 y,int32 width,int 32 height);

【问题讨论】:

  • 您确实意识到您实际上是在处理 Graphics 对象然后尝试再次使用它,对吗?
  • 啊抱歉,我在发帖后提到了,但现在我有另一个问题,如何在不同的显示器中使表单大小保持静态?由不同的维度?对不起,谢谢你
  • 使用CreateGraphics 几乎总是一个错误。你的 draw 方法有一个 PaintEventArgs 传递给它,我假设你是从某种 Paint 事件中得到的。您应该使用来自它的 Graphics 实例:Graphics gr = e.Graphics。也不要丢弃它。

标签: c# exception graphics drawing ellipse


【解决方案1】:

由于您传递的是PaintEventArgs e,因此您可以并且应该使用它的e.Graphics

既然不是您创建的,请不要丢弃它!

但是您创建的那些PensBrushes 应该被丢弃,或者更好的是,在using 子句中创建它们!对于SolidBrush,我们可以使用标准Brush,我们无法更改,也不得丢弃!

为了确保 Fill 不会覆盖 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);
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    相关资源
    最近更新 更多