public static void DrawCapsule(this Graphics g, Brush brush, Rectangle rect) { using (GraphicsPath gp = new GraphicsPath()) { gp.AddCapsule(rect); g.FillPath(brush, gp); } }
public static void AddRoundedRectangle(this GraphicsPath graphicsPath, RectangleF rect, float radius, float penWidth = 1) { if (radius <= 0f) { graphicsPath.AddRectangleProper(rect); } else { if (penWidth == 1) { rect = new RectangleF(rect.X, rect.Y, rect.Width - 1, rect.Height - 1); } // If the corner radius is greater than or equal to // half the width, or height (whichever is shorter) // then return a capsule instead of a lozenge if (radius >= (Math.Min(rect.Width, rect.Height) / 2.0f)) { graphicsPath.AddCapsule(rect); } else { // Create the arc for the rectangle sides and declare // a graphics path object for the drawing float diameter = radius * 2.0f; SizeF size = new SizeF(diameter, diameter); RectangleF arc = new RectangleF(rect.Location, size); // Top left arc graphicsPath.AddArc(arc, 180, 90); // Top right arc arc.X = rect.Right - diameter; graphicsPath.AddArc(arc, 270, 90); // Bottom right arc arc.Y = rect.Bottom - diameter; graphicsPath.AddArc(arc, 0, 90); // Bottom left arc arc.X = rect.Left; graphicsPath.AddArc(arc, 90, 90); graphicsPath.CloseFigure(); } } }
public static void AddRoundedRectangle(this GraphicsPath gp, RectangleF rect, float radius) { if (radius <= 0f) { gp.AddRectangle(rect); } else { if (radius >= (Math.Min(rect.Width, rect.Height) / 2.0f)) { gp.AddCapsule(rect); } else { var diameter = radius * 2.0f; var size = new SizeF(diameter, diameter); var arc = new RectangleF(rect.Location, size); // Top left arc gp.AddArc(arc, 180, 90); // Top right arc arc.X = rect.Right - diameter; gp.AddArc(arc, 270, 90); // Bottom right arc arc.Y = rect.Bottom - diameter; gp.AddArc(arc, 0, 90); // Bottom left arc arc.X = rect.Left; gp.AddArc(arc, 90, 90); gp.CloseFigure(); } } }