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

...那么为什么后两个椭圆完全是白色的?...我怎样才能避免这个问题?

4

2 回答 2

3

这可能是众多 GDI+ 错误之一。这是由于抗锯齿与 DashPattern 相结合。不过有趣的是(嗯,有点……),如果你删除 SmoothingMode = AntiAlias,你会得到一个很棒的 OutOfMemoryException(如果你在“gdi+ pattern outofmemoryexception”上搜索,你会发现数百个。真是一团糟。

由于 GDI+ 并没有真正维护(尽管它也用于 .NET Framework Winforms,顺便说一句,我用 .NET C# 重现了您的问题),因为此链接可以告诉我们:Pen.DashPattern throw OutOfMemoryException using a default pen,您可以这样做的唯一方法可能解决这个问题是尝试各种值。

例如,如果您改为使用此更改 DashOffset 设置:

 myPen2->DashOffset = i*ellipseCircumference;

你会产生一组很好的椭圆,所以也许你能找到一个真正适合你的组合。祝你好运 :-)

于 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 回答