首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理静电刷

处理静电刷
EN

Stack Overflow用户
提问于 2009-01-23 02:22:54
回答 3查看 3.6K关注 0票数 3

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

代码语言:javascript
复制
myPictureBox.Image = GetBiorhythm2();

这是第一次运行ok,,但是在第二次单击时,它会导致以下异常:

代码语言:javascript
复制
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

导致错误的缩减函数是:

代码语言:javascript
复制
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;
 }

如果我注释掉刷子处理,它的工作正常,但我不满意,并希望找到另一种解决方案。你能帮帮我吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-01-23 02:46:04

看起来,您正在尝试处理一个静态,这将在下次使用它时引起一些问题:

代码语言:javascript
复制
    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.

设置画笔= Brushes.Black时,实际上是将画笔设置为对静态Brushes.Black的引用(或指针)。通过处理它,您可以有效地编写:

代码语言:javascript
复制
    Brushes.Black.dispose();

当您再次返回使用黑色画笔时,运行时表示不能使用,因为它已经被释放,并且不是g.FillEllipse()的有效参数。

写这篇文章的一个更好的方法可能只是简单地:

代码语言:javascript
复制
    g.FillEllipse(Brushes.Black, 3, 3, 2, 2);

或者,如果你想变得很复杂的话:

代码语言:javascript
复制
    Brush brush = Brushes.Black.Clone();
    g.FillEllipse( brush, 3, 3, 2, 2 );
    brush.Dispose();

或者,如果您不关心事情看上去有问题,只需在原始代码中注释掉brush.Dispose();行。

票数 11
EN

Stack Overflow用户

发布于 2009-01-23 02:45:17

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

笔刷类的文档如下:

--画笔类包含静态只读属性,这些属性返回由属性名称指示的颜色的画笔对象。您通常不需要显式地释放这个类中的属性返回的画笔,除非它用于构造一个新的画笔。

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

票数 14
EN

Stack Overflow用户

发布于 2009-01-23 02:25:46

我不认为您需要在静态画笔上调用.Dispose,除非您创建了新的画笔。虽然,就我个人而言,我会使用使用语法。ie:

代码语言:javascript
复制
using (Brush brush = new SolidBrush(...))
{
    g.FillEllipse(brush, 3, 3, 2, 2);
}

你应该对你创建的图形对象做同样的事情。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/471662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档