繁体   English   中英

Graphics.FillPath()的问题行为

[英]Problematic Behavior of Graphics.FillPath()

我创建了一个小函数,绘制一个边缘更细的Rectangle。 (你可以称之为圆角矩形)

我是这样做的:

private void    DrawRoundedRectangle(Graphics G, int X1, int Y1, int X2, int Y2)
{
    GraphicsPath    GP  =new GraphicsPath();
    GP.AddLine(X1+1,Y1  ,  X2-1,Y1  );
    GP.AddLine(X2-1,Y1  ,  X2  ,Y1+1);
    GP.AddLine(X2  ,Y1+1,  X2  ,Y2-1);
    GP.AddLine(X2  ,Y2-1,  X2-1,Y2  );
    GP.AddLine(X2-1,Y2  ,  X1+1,Y2  );
    GP.AddLine(X1+1,Y2  ,  X1  ,Y2-1);
    GP.AddLine(X1  ,Y2-1,  X1  ,Y1+1);
    GP.AddLine(X1  ,Y1+1,  X1+1,Y1  );

    G.DrawPath(Pens.Blue,GP);
}

这是调用此函数的Paint事件处理程序:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    this.DrawRoundedRectangle(e.Graphics,50,50,60,55);
}

运行它,确实给出了期望的结果,这是:

我希望得到一个好结果。

但是,如果我改变了
G.DrawPath(Pens.Blue,GP);
行是:
G.FillPath(Brushes.Blue,GP);
然后我得到的是这个:

不是我想要的结果..
矩形的底部是锐利的,并且根据需要不是圆形,就像使用DrawPath()方法一样。

任何人都知道我应该做些什么来使FillPath()方法也运作良好?
如果重要,我使用的是.NET Framework 2.0。

如果你想要一个真正的 round -rect实现,你应该使用commenter blas3nik引用的问题中的代码,在Oddly 使用Graphics.FillPath绘制GraphicsPath

您的实现主要是删除四个角上的每个像素。 因此,不需要使用GraphicsPath来绘制它。 只需填充几个排除这些像素的重叠矩形:

    private void FillRoundedRectangle(Graphics G, int X1, int Y1, int X2, int Y2)
    {
        int width = X2 - X1, height = Y2 - Y1;

        G.FillRectangle(Brushes.Blue, X1 + 1, Y1, width - 2, height);
        G.FillRectangle(Brushes.Blue, X1, Y1 + 1, width, height - 2);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM