-
19-08-2019 - |
문제
바이오 리듬 앱을 쓰고 있습니다. 테스트하기 위해 버튼과 그림 상자가있는 양식이 있습니다. 버튼을 클릭하면
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;
}
브러시 처리에 댓글을 달면 잘 작동하지만 그에 만족하지 않고 대체 솔루션을 찾고 싶습니다. 도와 줄수있으세요 ?
해결책
정적을 폐기하려고하는 것처럼 보이며, 다음에 사용될 때 몇 가지 문제가 발생합니다.
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.
Brush = 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 ()에 주석하십시오. 원래 코드로 줄을 긋습니다.
다른 팁
Bruhes.black은 시스템 리소스이며 귀하가 처분하기위한 것이 아닙니다. 런타임은 브러시 클래스, 펜 및 기타 객체의 브러시를 관리합니다. 그것은 필요에 따라 해당 객체를 생성하고 처분하여 자주 사용하는 품목을 살아있게 유지하여 지속적으로 만들고 파괴 할 필요가 없습니다.
Brushes 클래스의 문서는 다음과 같습니다.
브러시 클래스에는 속성 이름으로 표시된 색상의 브러시 객체를 반환하는 정적 읽기 전용 속성이 포함되어 있습니다. 일반적으로 새 브러시를 구성하는 데 사용되지 않는 한이 클래스의 속성에 의해 반환 된 브러시를 명시 적으로 처분 할 필요는 없습니다.
요컨대, 시스템이 부여 된 객체를 처리하는 것을 호출하지 마십시오.
나는 당신이 새 브러쉬를 만들 때만 정적 브러시를 부를 필요는 없다고 생각합니다. 개인적으로는 사용 구문을 사용합니다. 즉 : IE :
using (Brush brush = new SolidBrush(...))
{
g.FillEllipse(brush, 3, 3, 2, 2);
}
그리고 당신은 아마 당신이 만든 그래픽 객체와 동일한 작업을 수행해야 할 것입니다.