C# GDI+ 绘制圆角实心矩形

            /// <summary> 
            /// C# GDI+ 绘制圆角实心矩形 
            /// </summary> 
            /// <param name="g">Graphics 对象</param> 
            /// <param name="rectangle">要填充的矩形</param> 
            /// <param name="backColor">填充背景色</param> 
            /// <param name="r">圆角半径</param> 
            public static void FillRoundRectangle(Graphics g, Rectangle rectangle, Color backColor, int r)
            {
                rectangle = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width - 1, rectangle.Height - 1);
                Brush b = new SolidBrush(backColor);
                g.FillPath(b, GetRoundRectangle(rectangle, r));
            }

            /// <summary> 
            /// 根据普通矩形得到圆角矩形的路径 
            /// </summary> 
            /// <param name="rectangle">原始矩形</param> 
            /// <param name="r">半径</param> 
            /// <returns>图形路径</returns> 
            private static GraphicsPath GetRoundRectangle(Rectangle rectangle, int r)
            {
                int l = 2 * r;
                // 把圆角矩形分成八段直线、弧的组合,依次加到路径中 
                GraphicsPath gp = new GraphicsPath();
                gp.AddLine(new Point(rectangle.X + r, rectangle.Y), new Point(rectangle.Right - r, rectangle.Y));
                gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Y, l, l), 270F, 90F);

                gp.AddLine(new Point(rectangle.Right, rectangle.Y + r), new Point(rectangle.Right, rectangle.Bottom - r));
                gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Bottom - l, l, l), 0F, 90F);

                gp.AddLine(new Point(rectangle.Right - r, rectangle.Bottom), new Point(rectangle.X + r, rectangle.Bottom));
                gp.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - l, l, l), 90F, 90F);

                gp.AddLine(new Point(rectangle.X, rectangle.Bottom - r), new Point(rectangle.X, rectangle.Y + r));
                gp.AddArc(new Rectangle(rectangle.X, rectangle.Y, l, l), 180F, 90F);
                return gp;
            }

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值