0

I have implemented a rounded rectangle extension method, defined here.

public static Graphics DrawRRectangle(this Graphics g, Pen p, int x, int y, int width, int height, int feathering)
    {
        g.DrawLine(p, x, y + feathering, x, y + height - feathering);
        g.DrawBezier(p, new Point(x, y + height - feathering),
                        new Point(x, y + height - feathering / 2), new Point(x + feathering / 2, y + height),
                        new Point(x + feathering, y + height));

        g.DrawLine(p, x + feathering, y + height , x + width - feathering, y + height);
        g.DrawBezier(p, new Point(x + width - feathering, y + height),
                        new Point(x + width - feathering / 2, y + height), new Point(x + width, y + height - feathering / 2),
                        new Point(x + width, y + height - feathering));

        g.DrawLine(p, x + width, y + height - feathering, x + width, y + feathering);
        g.DrawBezier(p, new Point(x + width, y + feathering),
                        new Point(x + width, y + feathering / 2), new Point(x + width - feathering / 2, y),
                        new Point(x + width - feathering, y));

        g.DrawLine(p, x + width - feathering, y, x + feathering, y);
        g.DrawBezier(p, new Point(x + feathering, y),
                        new Point(x + feathering / 2, y), new Point(x, y + feathering / 2),
                        new Point(x, y + feathering));
        return g;
    }

However when I use this method like so g.DrawRRectangle(p, 100, 100, 1000, 1000, 100);, I do not get the outcome I wanted, each of the corners are either misaligned of their pixels do not match up As seen in the images below.
Bottom left corner of rounded rectangle Top left corner of rounded rectangle
Top right corner of rounded rectangle Bottom right corner of rounded rectangle

Any suggestions anybody could offer would be helpful, I am unsure if this is a problem with the equations used to generate my curves however this is the first time I am dabbling with graphics, so it could just be my thinking. Thanks.

Curtis
  • 37
  • 9
  • There are better ways to draw rounded rectangles... – Matthew Layton May 02 '17 at 10:38
  • https://web.archive.org/web/20111110163706/http://www.switchonthecode.com/tutorials/csharp-creating-rounded-rectangles-using-a-graphics-path – Matthew Layton May 02 '17 at 10:38
  • http://stackoverflow.com/questions/19167463/oddly-drawn-graphicspath-with-graphics-fillpath – Matthew Layton May 02 '17 at 10:38
  • I tried that first links implementation however I am confused about this enum logic, I get errors with the syntax for each of the enum conditionals. `RectangleCorners.TopLeft & corners == RectangleCorners.TopLeft` it says that the & operand cannot be applied – Curtis May 02 '17 at 12:21
  • an enum is a poor way to determine which corners should be rounded. Try the second example instead as this uses a BorderRadius class, which allows you to specify the rounding amount for each corner individually. – Matthew Layton May 02 '17 at 12:23
  • Thanks, you can provide the second link as an answer and say we discussed it in the comments if you want the accepted answer. – Curtis May 02 '17 at 13:16
  • `RectangleCorners.TopLeft & corners == RectangleCorners.TopLeft` works if you wrap the first logical and operation in parenthesis: `(RectangleCorners.TopLeft & corners) == RectangleCorners.TopLeft` Or you could use `Enum.HasFlag` https://msdn.microsoft.com/en-us/library/system.enum.hasflag(v=vs.110).aspx – Matthew Layton May 02 '17 at 13:20

1 Answers1

2

Whilst I can't comment on your implementation, you're going to run into problems further down the road with this. Your implementation will give the appearance of drawing a rounded rectangle, but say for example in future you want to fill the shape, you won't be able to because GDI/GDI+ won't see the drawn shapes as a single consecutive shape.

In this respect you should use a GraphicsPath.

See here for a complete solution for drawing rounded rectangles using a GraphicsPath.

Community
  • 1
  • 1
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313