我正在写一个生物节律应用程序.
为了测试它,我有一个带有Button和PictureBox的表单.
当我点击按钮时,我做了
为了测试它,我有一个带有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:\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,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();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。