Drawing/Filling Rounded Rectangles

Posted by: s.mcchesney on 13 July 2018, 1:23 am EST

    • Post Options:
    • Link

    Posted 13 July 2018, 1:23 am EST

    I have a need in one of my documents to generate filled rounded rectangles. I see the

    RoundRectFigure
    class is available in the library, but I cannot see how to get that into some sort of Path that the
    Page.Graphics
    property can manage for Fill methods. (```

    PathBuilder

    
    Any help you can give me would be great.  Thanks!
    
    - Scott McChesney
  • Posted 15 July 2018, 7:32 pm EST

    Hello Scott,

    We are discussing this requirement with the team and will let you know once it is done.

    Thanks,

    Esha

  • Posted 16 July 2018, 1:03 am EST

    Hi Scott,

    As you noticed, there are no Draw/FillRoundRect methods in the library yet. As a matter of fact we already have them in the development branch, they will be included in the next major update which will be available in the fall. For now, the only way is to use graphic paths.

    Thanks.

  • Posted 17 July 2018, 2:31 am EST

    Thank you for the update.

    This may also already be in your fall release, but I’m finding that the “FillPath” method does not fill paths with arc segments properly. It does not fill the arc; it basically draws a line from the start point to the end point and fills that. It’s entirely possible that I’m constructing the arc wrong, as I can’t really find any documentation on it. But I tried several different methods, and I got the same results.

    Because of these issues, we have had to revert to rectangles for our forms.

    • Scott McChesney
  • Posted 18 July 2018, 4:25 am EST

    Hi again,

    Attached is a modified Shapes sample, it adds a MakeRoundRect method that creates an IPath describing the specified round rectangle, and draws one on the first page. The actual code is this:

    private IPath MakeRoundRect(GcGraphics g, RectangleF rc, float rx, float ry)
    {
        var path = g.CreatePath();
        var sz = new SizeF(rx, ry);
        // start from horizontal top left
        path.BeginFigure(new PointF(rc.Left + rx, rc.Top));
        path.AddLine(new PointF(rc.Right - rx, rc.Top));
        path.AddArc(new ArcSegment() { Point = new PointF(rc.Right, rc.Top + ry), SweepDirection = SweepDirection.Clockwise, Size = sz });
        path.AddLine(new PointF(rc.Right, rc.Bottom - ry));
        path.AddArc(new ArcSegment() { Point = new PointF(rc.Right - rx, rc.Bottom), SweepDirection = SweepDirection.Clockwise, Size = sz });
        path.AddLine(new PointF(rc.Left + rx, rc.Bottom));
        path.AddArc(new ArcSegment() { Point = new PointF(rc.Left, rc.Bottom - ry), SweepDirection = SweepDirection.Clockwise, Size = sz });
        path.AddLine(new PointF(rc.Left, rc.Top + ry));
        path.AddArc(new ArcSegment() { Point = new PointF(rc.Left + rx, rc.Top), SweepDirection = SweepDirection.Clockwise, Size = sz });
        path.EndFigure(FigureEnd.Closed);
        return path;
     }
    
    

    and it can be used as follows:

    RectangleF rc = new RectangleF(72, 72, 72 * 4, 72 * 2);
    IPath path = MakeRoundRect(g, rc, 36, 24);
    g.FillPath(path, Color.Purple);
    g.DrawPath(path, new Pen(Color.Red, 4));
    
    

    Hope this works for you. I am not sure I understand correctly what the other issue you mention is, if you still have that problem please provide a sample code and/or the (wrong) result.

    Thanks.

    RoundRect.zip

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels