GraphicsPath.AddArc角度异常致WinForms控件圆角不均问题
控件圆角绘制边角不均匀问题
我在实现控件圆角方法时遇到了边角不均匀的问题:左上圆角显示正常,但右上、左下圆角变形,右下圆角变形严重,且控件右侧和底部各缺失一个像素。
我的具体实现代码如下:
GraphicsPath GetRoundedPath(Rectangle r, int radius, bool topRight = true, bool topLeft = true, bool bottomLeft = true, bool bottomRight = true) { GraphicsPath path = new GraphicsPath(); if (topLeft) path.AddArc(r.X, r.Y, radius, radius, 180, 90); else path.AddLine(r.X, r.Y, r.Right, r.Y); if (topRight) path.AddArc(r.Right - radius, r.Y, radius, radius, 270, 90); else path.AddLine(r.Right, r.Y, r.Right, r.Height); if (bottomRight) path.AddArc(r.Right - radius, r.Bottom - radius, radius, radius, 0, 90); else path.AddLine(r.Right, r.Bottom, r.Right, r.Height); if (bottomLeft) path.AddArc(r.X, r.Bottom - radius, radius, radius, 90, 90); else path.AddLine(r.X, r.Bottom, r.X, r.Y); return path; }
调用该方法设置Panel控件Region的代码:
Panel p = new Panel(); p.BackColor = Color.Red; p.Size = new Size(200, 200); Controls.Add(p); p.Region = new Region(GetRoundedPath(new Rectangle(p.ClientRectangle, 30, true, true, true, true)); p.Location = new Point(200, 200);
我尝试过以下几种解决方案,但都没解决问题:
- 传入缩小1像素的Rectangle:控件仅整体缩小1像素,圆角问题依旧;
- 改用
Graphics.FillPath绘制圆角:效果无变化,结合第一种方案也无效; - 单独增大部分圆角半径:边角被拉伸成斜线,效果更差。
另外我找到一篇讨论类似GraphicsPath.AddArc()异常行为的帖子,但该方案仅适用于Region且全圆角场景,无法解决我的GraphicsPath相关问题。
内容的提问来源于stack exchange,提问作者Yotic
相关产品推荐
相关产品推荐

