/// <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 AddRectangleProper(this GraphicsPath graphicsPath, RectangleF rect, float penWidth = 1) { if (penWidth == 1) { rect = new RectangleF(rect.X, rect.Y, rect.Width - 1, rect.Height - 1); } if (rect.Width > 0 && rect.Height > 0) { graphicsPath.AddRectangle(rect); } }
public static void AddRoundedRectangle(this GraphicsPath graphicsPath, RectangleF rect, float radius) { if (radius <= 0f) { graphicsPath.AddRectangle(rect); } else { // 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 path, RectangleF rect, int cornerRadius) { if (cornerRadius > 0) { path.StartFigure(); path.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90); path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y); path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90); path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2); path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90); path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom); path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90); path.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2); path.CloseFigure(); } else { path.AddRectangle(rect); } }
/// <summary> /// Adds a rectangle to the path at the specified <paramref name="location"/> /// </summary> /// <param name="path">Path to add the rectangle to</param> /// <param name="location">Location of the rectangle</param> public static void AddRectangle (this IGraphicsPath path, RectangleF location) { path.AddRectangle (location.X, location.Y, location.Width, location.Height); }
public static void AddRectangleProper(this GraphicsPath graphicsPath, RectangleF rect) { rect = new RectangleF(rect.X, rect.Y, rect.Width - 1, rect.Height - 1); if (rect.Width > 1 && rect.Height > 1) { graphicsPath.AddRectangle(rect); } }