DrawRoundRectangle does not fit in canvas

Drawing roundRectangles in a canvas causes some problems, as shown in the picture:
Screenshot 2024-10-09 at 18.44.08

The three rectangles above are drown in the paint event of the window:
g.DrawingColor = &cff0000
g.PenSize = 5
g.DrawRoundRectangle(10,10, 100, 100, 0, 0)
g.DrawRoundRectangle(120, 10, 100, 100, 16, 16)
g.DrawRoundRectangle(230, 10, 100, 100, 32, 32)

Already here one may notice that the last two rectangles are higher than the first one.

The three below are drown in the paint event of a canvas with Hight set to 100 (code below). And they clearly show the problem I’m facing; since the last two round rectangles get truncated at the top and at the bottom. And it does nor sound reasonable to draw round rectangles (100x100) at (100x104).
I hope I’m missing something, because if I cant find a solution I have to rely on some concocted code that tries to adjust the two mangled rectangles.
BTW: I tried also GraphicPath, but it does not solve the issue.

Suggestions welcome. Thanks.

g.DrawingColor = &cff0000
g.PenSize = 5
g.DrawRoundRectangle(10,0, 100, g.Height, 0, 0)
g.DrawRoundRectangle(120, 0, 100, g.Height, 16, 16)
g.DrawRoundRectangle(230, 0, 100, g.Height, 32, 32)

{added zipped project}
testCanvas.xojo_binary_project.zip (4.3 KB)

Rectangle Height: 100
PenSize: 5
Total: 105

Canvas Height: 100

Think of the “pen” drawing the 5 pixel line perfectly centered along the edges of the rectangle you are describing. Half of the pixels are on the inside of the lube, half on the outside.

To make the drawing fit, you’ll need to take this into account by moving it down and to the left half of the pen width, and then reduce the height and width by the full pen width.

hello
you have to add half of the pensize
also note the draw line

g.DrawingColor = &cff0000

g.PenSize = 50

g.drawline(100,300,300,300)

g.PenSize = 40

Var diff As Double = (50-40)/2

g.DrawingColor = &cffff00

g.drawline(100+diff,300+diff,300+diff,300+diff)

Var br As Double = 30

g.DrawingColor = &cffff00f0

For i As Integer = 1 To br

g.PenSize = i

g.drawline(50+br-i,350+br-i/2,350+br+i-i,350+br-i/2)

NextPreformatted text

@Emile_Schwarz @Eric_Williams @RudolfJ Thank you all for the quick response. I’d like to set all the three answers as the solution. Since it is not possible I’ll mark as solution Eric’s answer.