首页 > 代码库 > C# 画圆角矩形

C# 画圆角矩形

            int width = panel2.Width;
            int height = panel2.Height;
            int lineWeight = 10;//边框宽
            int radio = 30;//圆周半径
            Graphics g = panel2.CreateGraphics();
            //panel1.BackColor = System.Drawing.Color.Transparent;
            Bitmap bm = new Bitmap(width, width);
            Graphics grs = Graphics.FromImage(bm);
            grs.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            grs.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            grs.Clear(Color.White);
            int padding = 15;//向内缩进
            //左上圆角
            grs.FillPie(Brushes.LightSalmon, padding, padding, radio * 2, radio * 2, 180, 90);
            //左下
            grs.FillPie(Brushes.LightSalmon, padding, height - 2 * radio - padding, radio * 2, radio * 2, 90, 90);
            //右上
            grs.FillPie(Brushes.LightSalmon, width - 2 * radio - padding, padding, radio * 2, radio * 2, 270, 90);
            //右下
            grs.FillPie(Brushes.LightSalmon, width - 2 * radio - padding, height - 2 * radio - padding, radio * 2, radio * 2, 0, 90);

            //竖向矩形
            grs.FillRectangle(Brushes.LightSalmon, radio + padding - 1, padding, width - 2 * radio - 2 * padding + 2, height - 2 * padding);
            //横向矩形
            grs.FillRectangle(Brushes.LightSalmon, padding, radio - 1 + padding, width - 2 * padding, height - 2 * radio - 2 * padding + 2);

            if (lineWeight > 0)
            {
                Pen p = new Pen(Brushes.Blue, lineWeight);
                //左上弧
                grs.DrawArc(p, padding, padding, radio * 2, radio * 2, 180, 90);
                //左下
                grs.DrawArc(p, padding, height - 2 * radio - padding, radio * 2, radio * 2, 90, 90);
                //右上
                grs.DrawArc(p, width - 2 * radio - padding, padding, radio * 2, radio * 2, 270, 90);
                //右下
                grs.DrawArc(p, width - 2 * radio - padding, height - 2 * radio - padding, radio * 2, radio * 2, 0, 90);

                //上框
                grs.DrawLine(p, radio + padding - 1, padding, width - radio - padding + 2, padding);
                //下
                grs.DrawLine(p, radio + padding - 1, height - padding, width - radio - padding + 2, height - padding);
                //左
                grs.DrawLine(p, padding, radio + padding - 1, padding, height - radio - padding + 2);
                //右
                grs.DrawLine(p, width - padding, radio + padding - 1, width - padding, height - radio - padding + 2);
            }


            g.DrawImage(bm, 0, 0);
            grs.Dispose();
            bm.Dispose();
            g.Dispose();

C# 画圆角矩形