4

私は(単純に)楕円のパスに沿って回転するいくつかの線を描こうとしていますが、それを行う簡単な方法があると思いました。残念ながら、私の解決策にはいくつかの問題があるようです:

void EllipseDisplayControl::OnPaint(PaintEventArgs^ e)
{
    Graphics^ gfx = e->Graphics;
    gfx->SmoothingMode = Drawing2D::SmoothingMode::AntiAlias;

    int width = 100;
    int height = 10;

    for( int i = 0; i < 15; i ++ )
    {
        Drawing::Pen^ myPen = (Drawing::Pen^) Drawing::Pens::RoyalBlue->Clone(); //use the standard blue as a start point
        myPen->Color = Drawing::Color::FromArgb(64, 32, 111, 144);
        myPen->Width = 3;
        myPen->DashStyle = Drawing::Drawing2D::DashStyle::Solid;
        gfx->DrawEllipse(myPen, 0, 50+i*20, width, height); // Draw the blue ring

        float ellipseCircumference = Math::PI * Math::Sqrt(2* (Math::Pow(0.5*width,2) + Math::Pow(0.5*height,2)));
        array<Single>^ pattern = {4, ellipseCircumference};

        Drawing::Pen^ myPen2 = (Drawing::Pen^) Drawing::Pens::White->Clone(); //use the standard blue as a start point
        myPen2->DashPattern = pattern;
        myPen2->DashOffset = i*10;
        gfx->DrawEllipse(myPen2, 0, 50+i*20, width, height); // Draw the rotating white dot
    }
}

...生成します:

http://www.joncage.co.uk/media/img/BadPattern.png

...では、なぜ 2 番目の 2 つの楕円は完全に白いのでしょうか? ...どうすれば問題を回避できますか?

4

2 に答える 2

3

これはおそらく、数多くある GDI+ のバグの 1 つです。これは、DashPattern と組み合わせた Antialiasing によるものです。面白いですが (まあ、ちょっと...)、SmoothingMode = AntiAlias を削除すると、すばらしい OutOfMemoryException が発生します (そして、「gdi+ pattern outofmemoryexception」でググると、何百ものこれらが見つかります。

GDI+ は実際には維持されていないため (.NET Framework Winforms でも使用されていますが、.NET C# で問題を再現しました)、このリンクからわかるように: Pen.DashPattern throw OutOfMemoryException using a default pen , the only way you canおそらくこれを回避するには、さまざまな値を試すことです。

たとえば、代わりにこれを使用して DashOffset 設定を変更すると、次のようになります。

 myPen2->DashOffset = i*ellipseCircumference;

素敵な楕円のセットが作成されるので、本当に自分に合った組み合わせを 1 つ見つけることができるかもしれません。幸運を :-)

于 2010-12-21T11:25:45.170 に答える
1

問題が解決するとは思えませんが、ループから多くの処理を取り除くことができます。

void EllipseDisplayControl::OnPaint(PaintEventArgs^ e)
{
    Graphics^ gfx = e->Graphics;
    gfx->SmoothingMode = Drawing2D::SmoothingMode::AntiAlias;

    int width = 100;  
    int height = 10;

    Drawing::Pen^ myPen = (Drawing::Pen^) Drawing::Pens::RoyalBlue->Clone(); //use the standard blue as a start point
    myPen->Color = Drawing::Color::FromArgb(64, 32, 111, 144);
    myPen->Width = 3;
    myPen->DashStyle = Drawing::Drawing2D::DashStyle::Solid;

    float ellipseCircumference = Math::PI * Math::Sqrt(2* (Math::Pow(0.5*width,2) + Math::Pow(0.5*height,2)));
    array<Single>^ pattern = {4, ellipseCircumference};

    Drawing::Pen^ myPen2 = (Drawing::Pen^) Drawing::Pens::White->Clone(); //use the standard blue as a start point
    myPen2->DashPattern = pattern;

    for( int i = 0; i < 15; i ++ )
    {
        gfx->DrawEllipse(myPen, 0, 50+i*20, width, height); // Draw the blue ring

        myPen2->DashOffset = i*10;
        gfx->DrawEllipse(myPen2, 0, 50+i*20, width, height); // Draw the rotating white dot
    }
}
于 2010-12-21T11:06:09.370 に答える