加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 处理静电刷

发布时间:2020-12-15 20:50:40 所属栏目:百科 来源:网络整理
导读:我正在写一个生物节律应用程序. 为了测试它,我有一个带有Button和PictureBox的表单. 当我点击按钮时,我做了 myPictureBox.Image = GetBiorhythm2(); 哪个第一次运行正常,但在第二次单击时会导致以下异常: System.ArgumentException: Parameter is not valid
我正在写一个生物节律应用程序.
为了测试它,我有一个带有Button和PictureBox的表单.
当我点击按钮时,我做了

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:deloHoroskopBiorhythm.cs:line 157
   at Larifari.test.Button1Click in c:deloHoroskoptest.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:deloHoroskoptest.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,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;
 }

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

解决方法

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

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

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

当你设置brush = Brushes.Black时,你实际上是将画笔设置为静态Brushes.Black的引用(或指针).通过处理它,你有效地写作:

Brushes.Black.dispose();

当你回来再次使用黑色画笔时,运行时说你不能,因为它已经被处理掉了,并且不是g.FillEllipse()的有效参数

写这个的更好方法可能只是:

g.FillEllipse(Brushes.Black,2);

或者,如果你想要真的很复杂:

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

或者如果你不关心看错的东西,只需注释掉brush.Dispose();原始代码中的行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读