質問

バイオリズムアプリを書いています。 それをテストするために、ボタンとPictureBoxのあるフォームがあります。 ボタンをクリックすると

myPictureBox.Image = GetBiorhythm2();

初回は正常に実行されますが、 2回目のクリックでは次の例外が発生します:

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への参照(またはポインター)としてブラシを設定しています。破棄することで、次のように効果的に記述できます。

    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クラスのブラシ、ペン、およびその他のそのようなオブジェクトを管理します。必要に応じてこれらのオブジェクトを作成および破棄し、頻繁に使用するアイテムを存続させ、それらを継続的に作成および破棄する必要がないようにします。

Brushesクラスのドキュメントには次のように書かれています:

  

Brushesクラスにはstatic   を返す読み取り専用プロパティ   によって示される色のブラシオブジェクト   プロパティ名。通常は   明示的に処分する必要はありません   これのプロパティによって返されるブラシ   クラス、構築に使用されない限り   新しいブラシ。

要するに、システム提供のオブジェクトでDisposeを呼び出さないでください。

静的ブラシで.Disposeを呼び出す必要はないと思います。新しいブラシを作成する場合のみです。個人的には、使用構文を使用します。つまり:

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

そして、おそらくあなたが作成したグラフィックスオブジェクトで同じことをすべきでしょう。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top