处理静电刷

发布于 2024-07-13 04:21:47 字数 1712 浏览 8 评论 0原文

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

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;
 }

如果我注释掉刷子处理工作正常,但我对此不满意并希望找到替代解决方案。 你能帮我吗 ?

I'm writing a biorhythm app.
To test it i have a form with a Button and a PictureBox.
When I click on the button i do

myPictureBox.Image = GetBiorhythm2();

Which runs ok for the first time, but on the second click it causes the following exception:

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

the cut-down function which causes the error is :

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;
 }

if I comment-out the brush disposal it works ok, but I am not happy with that and wish to find an alternative solution. Can you help me please ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

为人所爱 2024-07-20 04:21:47

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

Brushes 类的文档说:

Brushes 类包含静态
返回 a 的只读属性
颜色由 指示的画笔对象
属性名称。 你通常会这样做
不必明确处置
由属性返回的刷子
类,除非它用于构造
一把新画笔。

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

Bruhes.Black is a system resource, and is not intended for you to dispose. The runtime manages the brushes in the Brushes class, the Pens, and other such objects. It creates and disposes those objects as required, keeping frequently-used items alive so that it doesn't have to continually create and destroy them.

The documentation for the Brushes class says:

The Brushes class contains static
read-only properties that return a
Brush object of the color indicated by
the property name. You typically do
not have to explicitly dispose of the
brush returned by a property in this
class, unless it is used to construct
a new brush.

In short, do not call Dispose on the system-supplied objects.

叫思念不要吵 2024-07-20 04:21:47

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

    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 的引用(或指针) 。 通过处理它,您可以有效地编写:

    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();

或者如果你不关心看起来错误的事情,只需注释掉 Brush.Dispose(); 原始代码中的行。

It looks like you're trying to dispose of a static, which causes some problems next time it's used:

    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.

When you set brush = Brushes.Black, you're actually setting brush as a reference (or pointer) to the static Brushes.Black. By disposing it, you're effectively writing:

    Brushes.Black.dispose();

When you come back around to use the black brush again, the runtime says you can't because it's already been disposed of, and isn't a valid argument to g.FillEllipse()

A better way to write this might be just simply:

    g.FillEllipse(Brushes.Black, 3, 3, 2, 2);

Or, if you want to be really complex about it:

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

Or if you don't care about things looking wrong, just comment out the brush.Dispose(); line in your original code.

尾戒 2024-07-20 04:21:47

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

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

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

I don't think you need to call .Dispose on static brushes, only if you create new ones. Although, personally, I would use the using syntax.. ie:

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

And you should probably do the same thing with the graphics object you create.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文