是否更好的方法在Paint事件中使用新的Brush,即
protected override void OnPaint(PaintEventArgs e) {
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using (SolidBrush b = new SolidBrush(Color.FromArgb(129, 242, 121))) {
for (int i = 0; i < 12; i++) {
e.Graphics.FillPath(b, path[i]);
}
}
base.OnPaint(e);
}
Run Code Online (Sandbox Code Playgroud)
或者在顶部定义一次并在Dispose方法中处理即
SolidBrush _brush;
protected SolidBrush Brush {
get {
if (_brush == null)
_brush = new SolidBrush(Color.FromArgb(129, 242, 121));
return _brush;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在写一个生物节律应用程序.为了测试它,我有一个带有Button和PictureBox的表单.当我点击按钮时,我做了
myPictureBox.Image = GetBiorhythm2();
Run Code Online (Sandbox Code Playgroud)
哪个第一次运行正常,但在第二次单击时会导致以下异常:
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
Run Code Online (Sandbox Code Playgroud)
导致错误的减少功能是:
public static Image GetBiorhythm2() {
Bitmap bmp = new Bitmap(600, 300);
Image img = bmp;
Graphics g = Graphics.FromImage(img);
Brush …Run Code Online (Sandbox Code Playgroud)