C# 处理静电刷

C# 处理静电刷,c#,asp.net,dispose,system.drawing,brush,C#,Asp.net,Dispose,System.drawing,Brush,我正在写一个生物节律应用程序。 为了测试它,我有一个带有按钮和图片盒的表单。 当我点击按钮时,我会这样做 myPictureBox.Image = GetBiorhythm2(); 第一次运行正常,但第二次单击时会导致以下异常: System.ArgumentException: Parameter is not valid. at System.Drawing.Graphics.CheckErrorStatus at System.Drawing.Graphics.FillEll

我正在写一个生物节律应用程序。 为了测试它,我有一个带有按钮和图片盒的表单。 当我点击按钮时,我会这样做

myPictureBox.Image = GetBiorhythm2();
第一次运行正常,但第二次单击时会导致以下异常:

System.ArgumentException: Parameter is not valid.
   at System.Drawing.Graphics.CheckErrorStatus
   at System.Drawing.Graphics.FillEllipse
   at Larifari.Biorhythm.Biorhythm.GetBiorhythm2 in c:\delo\Horoskop\Biorhythm.cs:line 157
   at Larifari.test.Button1Click in c:\delo\Horoskop\test.Designer.cs:line 169
   at System.Windows.Forms.Control.OnClick
   at System.Windows.Forms.Button.OnClick
   at System.Windows.Forms.Button.OnMouseUp
   at System.Windows.Forms.Control.WmMouseUp
   at System.Windows.Forms.Control.WndProc
   at System.Windows.Forms.ButtonBase.WndProc
   at System.Windows.Forms.Button.WndProc
   at ControlNativeWindow.OnMessage
   at ControlNativeWindow.WndProc
   at System.Windows.Forms.NativeWindow.DebuggableCallback
   at ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop
   at ThreadContext.RunMessageLoopInner
   at ThreadContext.RunMessageLoop
   at System.Windows.Forms.Application.Run
   at Larifari.test.Main in c:\delo\Horoskop\test.cs:line 20
导致错误的切断功能是:

public static Image GetBiorhythm2() {
        Bitmap bmp = new Bitmap(600, 300);
        Image img = bmp;
        Graphics g = Graphics.FromImage(img);

        Brush brush = Brushes.Black;
        g.FillEllipse(brush, 3, 3, 2, 2); //Here the exception is thrown on the second call to the function

        brush.Dispose(); //If i comment this out, it works ok.

        return img;
 }

如果我对刷子处理进行评论,它可以正常工作,但我对此不满意,希望找到替代解决方案。你能帮帮我吗?

我想你不需要打电话。只有在你创建新的刷子时,才可以使用静电刷子。虽然,就我个人而言,我会使用using语法。。即:

using (Brush brush = new SolidBrush(...))
{
    g.FillEllipse(brush, 3, 3, 2, 2);
}

您可能也应该对所创建的图形对象执行相同的操作。

Bruhes.Black是一种系统资源,不供您处理。运行库管理笔刷类中的笔刷、笔和其他此类对象。它根据需要创建和处理这些对象,使经常使用的项目保持活动状态,这样就不必不断地创建和销毁它们

笔刷类的文档说明:

笔刷类包含静态 返回 笔刷对象的颜色由指示 属性名。你通常是这样做的 不必显式地处理 此文件中的属性返回的笔刷 类,除非它用于构造 一把新刷子


简而言之,不要在系统提供的对象上调用Dispose。

看起来您正在尝试处理静态文件,这会在下次使用静态文件时导致一些问题:

    Brush brush = Brushes.Black;
    g.FillEllipse(brush, 3, 3, 2, 2); //Here the exception is thrown on the second call to the function

    brush.Dispose(); //If i comment this out, it works ok.
当您设置brush=brush.Black时,实际上是将brush设置为静态brush.Black的引用(或指针)。通过处理它,您实际上是在写:

    Brushes.Black.dispose();
当您再次使用黑色笔刷时,运行时会说您不能使用,因为它已经被处理,并且不是g.FillEllipse()的有效参数

写这篇文章的更好方法可能是:

    g.FillEllipse(Brushes.Black, 3, 3, 2, 2);
或者,如果你想变得非常复杂:

    Brush brush = Brushes.Black.Clone();
    g.FillEllipse( brush, 3, 3, 2, 2 );
    brush.Dispose();

或者,如果你不在乎事情看起来有什么不对劲,只需注释掉画笔即可;原始代码中的行。

是的,不要试图处理静电刷。我花了很长时间跟踪了这一次。你的代码不只是将画笔设置为静态画笔的引用,然后在使用块后处理它吗?不,他正在这里创建一个新画笔。更准确地说,不要在静态对象上调用dispose。