2010-11-24 1 views
4

私は楕円のパスに沿って回転する線を描くことを試みています。なぜ第二2つの楕円は完全に白です...Pen :: DashPatternを設定した楕円を描画しないと、期待どおりの結果が得られないのはなぜですか?

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

を:生産...

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 
    } 
} 

:残念ながら、私の解決策はいくつかの問題を持っているようですか? ...どうすれば問題を回避できますか?

+0

典型的なGDI +損失。 –

答えて

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 
    } 
} 
+0

コードは単なる例にすぎません。現実には、私は一度にその楕円の一つを描いています。私は問題を浮き彫りにするためにいくつかを追加しました。 –

3

これはおそらく、多くのGDI +のバグの一つです。これは、DashPatternと組み合わせたアンチエイリアス処理によるものです。あなたがSmoothingMode = AntiAliasを削除すると面白いですが(グッド、一種...)、あなたは素晴らしいOutOfMemoryExceptionを取得します(そして、 "gdi + pattern outofmemoryexception"にgoogleすると、何百ものものが見つかります)

GDI +は実際には維持されていません(.NET Framework Winformsでも使用されていますが、.NET C#で問題が再現されました)。Pen.DashPattern throw OutOfMemoryException using a default pen、おそらくこれを回避する唯一の方法は次のとおりです。さまざまな値を試して

たとえば、あなたが代わりにこれで設定DashOffsetを変更する場合:。

myPen2->DashOffset = i*ellipseCircumference; 

あなたは楕円の素敵なセットを作ります。本当にあなたに合った組み合わせを見つけることができます。幸運:-)

+0

問題は、このコードが正常に動作しているときに私は複数の異なる環境を持っていることです。私はいくつかの例で動作するものを見つけることができますが、私はそれらのすべてのために動作する必要があります。 –

+0

うーん...悪いね。 GDI +以外のものを使用できませんか? WPFのように? –

+0

私たちのGUIは、C++/Windows Formsを使って書かれているので、書き直す必要はありません。 –

関連する問題