/// <summary> /// Adds a round rectangle to the graphics path where the integer radius specified determines how rounded the rectangle should become. /// This can be thought of rounded arcs connected by straight lines. /// </summary> /// <param name="self"></param> /// <param name="bounds"></param> /// <param name="radius"></param> public static void AddRoundedRectangle(this GraphicsPath self, Rectangle bounds, int radius) { if (radius * 2 > bounds.Width) { self.AddEllipse(bounds); return; } if (radius <= 0) { self.AddRectangle(bounds); return; } int w = radius * 2; Rectangle br = new Rectangle(bounds.Right - w, bounds.Bottom - w, w, w); Rectangle bl = new Rectangle(bounds.Left, bounds.Bottom - w, w, w); Rectangle tl = new Rectangle(bounds.Left, bounds.Top, w, w); Rectangle tr = new Rectangle(bounds.Right - w, bounds.Top, w, w); self.AddArc(br, 0, 90F); self.AddLine(new Point(bounds.Right - radius, bounds.Bottom), new Point(bounds.Left + radius, bounds.Bottom)); self.AddArc(bl, 90F, 90F); self.AddLine(new Point(bounds.Left, bounds.Bottom - radius), new Point(bounds.Left, bounds.Top + radius)); self.AddArc(tl, 180F, 90F); self.AddLine(new Point(bounds.Left + radius, bounds.Top), new Point(bounds.Right - radius, bounds.Top)); self.AddArc(tr, 270F, 90F); self.AddLine(new Point(bounds.Right, bounds.Top + radius), new Point(bounds.Right, bounds.Bottom - radius)); self.CloseFigure(); }
public static void AddCapsule(this GraphicsPath graphicsPath, RectangleF rect) { float diameter; RectangleF arc; try { if (rect.Width > rect.Height) { // Horizontal capsule diameter = rect.Height; SizeF sizeF = new SizeF(diameter, diameter); arc = new RectangleF(rect.Location, sizeF); graphicsPath.AddArc(arc, 90, 180); arc.X = rect.Right - diameter; graphicsPath.AddArc(arc, 270, 180); } else if (rect.Width < rect.Height) { // Vertical capsule diameter = rect.Width; SizeF sizeF = new SizeF(diameter, diameter); arc = new RectangleF(rect.Location, sizeF); graphicsPath.AddArc(arc, 180, 180); arc.Y = rect.Bottom - diameter; graphicsPath.AddArc(arc, 0, 180); } else { // Circle graphicsPath.AddEllipse(rect); } } catch { graphicsPath.AddEllipse(rect); } graphicsPath.CloseFigure(); }
/// <summary> /// Adds an ellipse to the path at the specified <paramref name="location"/> /// </summary> /// <param name="path">Path to add the ellipse to</param> /// <param name="location">Location of the bounding rectangle of the ellipse</param> public static void AddEllipse (this IGraphicsPath path, RectangleF location) { path.AddEllipse (location.X, location.Y, location.Width, location.Height); }