How to Customize Corners of Rectangles in PowerPoint Presentations in C#?

Hi there,

I try to create a custom shape with geometry path. It’s like a rectangle with round corners. Unfortunately I cannot use the built in shape for that since I want to customize each corner individually. So I created a custom shape for that with the following GeometryPath:

        var childShape = shapes.AddAutoShape(
                                    ShapeType.Custom, convertedX, convertedY, 
                                    convertedWidth, convertedHeight);

        var geometryPath = new GeometryPath();

        var point1 = new PointF(settings.TopLeftCornerRadiusF, 0);
        var point2 = new PointF(convertedWidth - settings.TopRightCornerRadiusF, point1.Y);
        var point3 = new PointF(point2.X, point2.Y + settings.TopRightCornerRadiusF);
        var point4 = new PointF(point3.X + settings.TopRightCornerRadiusF, point3.Y);
        var point5 = new PointF(point4.X, convertedHeight - settings.BottomRightCornerRadiusF);
        var point6 = new PointF(point5.X - settings.BottomRightCornerRadiusF, point5.Y);
        var point7 = new PointF(point6.X, point6.Y + settings.BottomRightCornerRadiusF);
        var point8 = new PointF(settings.BottomLeftCornerRadiusF, point7.Y);
        var point9 = new PointF(point8.X, point8.Y - settings.BottomLeftCornerRadiusF);
        var point10 = new PointF(point9.X - settings.BottomLeftCornerRadiusF, point9.Y);
        var point11 = new PointF(point10.X, settings.TopLeftCornerRadiusF);
        var point12 = new PointF(point11.X + settings.TopLeftCornerRadiusF, point11.Y);

        geometryPath.MoveTo(point1);
        geometryPath.LineTo(point2);
        geometryPath.LineTo(point3);
        geometryPath.LineTo(point4);
        geometryPath.LineTo(point5);
        geometryPath.LineTo(point6);
        geometryPath.LineTo(point7);
        geometryPath.LineTo(point8);
        geometryPath.LineTo(point9);
        geometryPath.LineTo(point10);
        geometryPath.LineTo(point11);
        geometryPath.LineTo(point12);

        geometryPath.CloseFigure();

        childShape.SetGeometryPath(geometryPath);

The resulting shape looks like this (Green Shape):
Capture.JPG (15.9 KB)

The GeometryPath class provides an ArcTo(width, height, startAngle, sweepAngle) which is probably the proper method to accomplish this. But I don’t know where to insert the calls to this method to create my round corners.

@ChristianPlankl,
Welcome to our community! Thank you for your inquiry.

To use arcs for shape geometry, you should imagine what part of the circle you need and the direction (clockwise or counterclockwise). The following code example shows you how to round corners with different sizes inward of a shape. I hope this example will help you:

var shapeX = 20f;
var shapeY = 20f;
var shapeWidth = 300f;
var shapeHeight = 200f;

var leftTopSize = 50f;
var rightTopSize = 20f;
var rightBottomSize = 40f;
var leftBottomSize = 10f;

using (var presentation = new Presentation())
{
    var childShape = presentation.Slides[0].Shapes.AddAutoShape(
        ShapeType.Custom, shapeX, shapeY, shapeWidth, shapeHeight);

    var geometryPath = new GeometryPath();

    var point1 = new PointF(leftTopSize, 0);
    var point2 = new PointF(shapeWidth - rightTopSize, 0);
    var point3 = new PointF(shapeWidth, shapeHeight - rightBottomSize);
    var point4 = new PointF(leftBottomSize, shapeHeight);
    var point5 = new PointF(0, leftTopSize);

    geometryPath.MoveTo(point1);
    geometryPath.LineTo(point2);
    geometryPath.ArcTo(rightTopSize, rightTopSize, 180, -90);
    geometryPath.LineTo(point3);
    geometryPath.ArcTo(rightBottomSize, rightBottomSize, -90, -90);
    geometryPath.LineTo(point4);
    geometryPath.ArcTo(leftBottomSize, leftBottomSize, 0, -90);
    geometryPath.LineTo(point5);
    geometryPath.ArcTo(leftTopSize, leftTopSize, 90, -90);

    geometryPath.CloseFigure();

    childShape.SetGeometryPath(geometryPath);

    presentation.Save("output.pptx", SaveFormat.Pptx);
}

Result: output.png (4.1 KB)

More examples: Custom Shape
API Reference: GeometryPath Class

1 Like

Hi @Andrey_Potapov thanks for your answer. Do you also have an example where you can show how to draw the corners the opposite way?
I’ve added an image to make it clearer.Inkedoutput_LI.jpg (16.9 KB)

@ChristianPlankl,
To round the corners outward, please use the following code snippet:

//...
geometryPath.MoveTo(point1);
geometryPath.LineTo(point2);
geometryPath.ArcTo(rightTopSize, rightTopSize, -90, 90);
geometryPath.LineTo(point3);
geometryPath.ArcTo(rightBottomSize, rightBottomSize, 0, 90);
geometryPath.LineTo(point4);
geometryPath.ArcTo(leftBottomSize, leftBottomSize, 90, 90);
geometryPath.LineTo(point5);
geometryPath.ArcTo(leftTopSize, leftTopSize, 180, 90);
//...

Result: output2.png (4.1 KB)

1 Like