Curtis
Curtis

Reputation: 37

Joining bezier curve to other drawings

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.

Upvotes: 0

Views: 95

Answers (1)

Matthew Layton
Matthew Layton

Reputation: 42340

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.

Upvotes: 2

Related Questions