gpt4 book ai didi

c# - Graphics.FillPath() 的问题行为

转载 作者:行者123 更新时间:2023-11-30 17:40:34 29 4
gpt4 key购买 nike

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

这是我的做法:

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。

最佳答案

如果你想要一个真正的圆角矩形实现,你应该使用commenter blas3nik引用的问题中的代码, 在 Oddly drawn GraphicsPath with Graphics.FillPath .

您的实现主要只是移除了四个角上的每个像素。因此,无需使用 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);
}

关于c# - Graphics.FillPath() 的问题行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34224961/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com