1. 画矩形边
public static void DrawRoundRectangle(this Graphics graphics, Pen pen, RectangleF rectangle, float radiusX, float radiusY = 0) { if (radiusY == 0) { radiusY = radiusX; } var mode = graphics.SmoothingMode; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; #region 方法1 //float[] itXs = new float[3] { rectangle.X + radiusX, rectangle.X + rectangle.Width - 1 - radiusX, rectangle.X + rectangle.Width - 1 }; //float[] itYs = new float[3] { rectangle.Y + radiusY, rectangle.Y + rectangle.Height - 1 - radiusY, rectangle.Y + rectangle.Height - 1 }; ////上 //graphics.DrawLine(pen, itXs[0], rectangle.Y, itXs[1], rectangle.Y); ////下 //graphics.DrawLine(pen, itXs[0], itYs[2], itXs[1], itYs[2]); ////左 //graphics.DrawLine(pen, rectangle.X, itYs[0], rectangle.X, itYs[1]); ////右 //graphics.DrawLine(pen, itXs[2], itYs[0], itXs[2], itYs[1]); ////左上角 //graphics.DrawArc(pen, rectangle.X, rectangle.Y, 2 * radiusX, 2 * radiusY, 180, 90); ////右上角 //graphics.DrawArc(pen, itXs[1] - radiusX, rectangle.Y, 2 * radiusX, 2 * radiusY, 270, 90); ////左下角 //graphics.DrawArc(pen, rectangle.X, itYs[1] - radiusY, 2 * radiusX, 2 * radiusY, 90, 90); ////右下角 //graphics.DrawArc(pen, itXs[1] - radiusX, itYs[1] - radiusY, 2 * radiusX, 2 * radiusY, 0, 90); #endregion #region 方法2 GraphicsPath GraphicsPath1 = new GraphicsPath(); //x 坐标 float[] itXs = new float[3] { rectangle.X + radiusX, rectangle.X + rectangle.Width - 1 - radiusX, rectangle.X + rectangle.Width - 1 }; //y 坐标 float[] itYs = new float[3] { rectangle.Y + radiusY, rectangle.Y + rectangle.Height - 1 - radiusY, rectangle.Y + rectangle.Height - 1 }; //左边 GraphicsPath1.AddLine(rectangle.X, itYs[1], rectangle.X, itYs[0]); //左上角 GraphicsPath1.AddArc(rectangle.X, rectangle.Y, 2 * radiusX, 2 * radiusY, 180, 90); //上边 GraphicsPath1.AddLine(itXs[0], rectangle.Y, itXs[1], rectangle.Y); //右上角 GraphicsPath1.AddArc(itXs[1] - radiusX, rectangle.Y, 2 * radiusX, 2 * radiusY, -90, 90); //右边 GraphicsPath1.AddLine(itXs[2], itYs[0], itXs[2], itYs[1]); //右下角 GraphicsPath1.AddArc(itXs[1] - radiusX, itYs[1] - radiusY, 2 * radiusX, 2 * radiusY, 0, 90); //下边 GraphicsPath1.AddLine(itXs[1], itYs[2], itXs[0], itYs[2]); //左下角 GraphicsPath1.AddArc(rectangle.X, itYs[1] - radiusY, 2 * radiusX, 2 * radiusY, 90, 90); graphics.DrawPath(pen, GraphicsPath1); #endregion graphics.SmoothingMode = mode; }
2. 填充圆角矩形
public static void FillRoundRectangle(this Graphics graphics, Brush brush, RectangleF rectangle, float radiusX, float radiusY = 0) { if (radiusY == 0) { radiusY = radiusX; } var mode = graphics.SmoothingMode; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; GraphicsPath GraphicsPath1 = new GraphicsPath(); //x 坐标 float[] itXs = new float[3] { rectangle.X + radiusX, rectangle.X + rectangle.Width - 1 - radiusX, rectangle.X + rectangle.Width - 1 }; //y 坐标 float[] itYs = new float[3] { rectangle.Y + radiusY, rectangle.Y + rectangle.Height - 1 - radiusY, rectangle.Y + rectangle.Height - 1 }; //左边 GraphicsPath1.AddLine(rectangle.X, itYs[1], rectangle.X, itYs[0]); //左上角 GraphicsPath1.AddArc(rectangle.X, rectangle.Y, 2 * radiusX, 2 * radiusY, 180, 90); //上边 GraphicsPath1.AddLine(itXs[0], rectangle.Y, itXs[1], rectangle.Y); //右上角 GraphicsPath1.AddArc(itXs[1] - radiusX, rectangle.Y, 2 * radiusX, 2 * radiusY, -90, 90); //右边 GraphicsPath1.AddLine(itXs[2], itYs[0], itXs[2], itYs[1]); //右下角 GraphicsPath1.AddArc(itXs[1] - radiusX, itYs[1] - radiusY, 2 * radiusX, 2 * radiusY, 0, 90); //下边 GraphicsPath1.AddLine(itXs[1], itYs[2], itXs[0], itYs[2]); //左下角 GraphicsPath1.AddArc(rectangle.X, itYs[1] - radiusY, 2 * radiusX, 2 * radiusY, 90, 90); graphics.FillPath(brush, GraphicsPath1); graphics.SmoothingMode = mode; }