首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么用Pen::DashPattern集绘制椭圆不能产生预期的结果?

为什么用Pen::DashPattern集绘制椭圆不能产生预期的结果?
EN

Stack Overflow用户
提问于 2010-11-25 02:00:20
回答 2查看 592关注 0票数 4

我试着(简单地)画一些沿着椭圆路径旋转的线,我想我有一个很好的简单的方法。不幸的是,我的解决方案似乎有一些问题:

代码语言:javascript
运行
复制
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
    }
}

...produces:

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

为什么第二个椭圆是全白的?,...and,我怎么才能避免这个问题?

EN

回答 2

Stack Overflow用户

发布于 2010-12-21 19:25:45

这可能是众多GDI+错误中的一个。这是由于结合了抗锯齿和DashPattern。有趣的是(好吧,有点...),如果你删除SmoothingMode = AntiAlias,你会得到一个很棒的OutOfMemoryException (如果你在谷歌上搜索"gdi+ pattern outofmemoryexception“,你会找到成百上千的这样的异常。啊!怎么这么乱呀。

由于GDI+并没有真正得到维护(尽管它也用在.NET框架Winforms中,但我用.NET C#重现了您的问题),正如这个链接可以告诉我们的:Pen.DashPattern throw OutOfMemoryException using a default pen,解决这个问题的唯一方法就是尝试不同的值。

例如,如果您使用以下代码更改DashOffset设置:

代码语言:javascript
运行
复制
 myPen2->DashOffset = i*ellipseCircumference;

您将生成一组很好的省略号,所以也许您可以找到一个真正适合您的组合。祝你好运:-)

票数 3
EN

Stack Overflow用户

发布于 2010-12-21 19:06:09

我不能想象它会解决这个问题,但你可以在循环中减少很多处理:

代码语言:javascript
运行
复制
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
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4270015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档