Problem
Graphics.DrawRoundedRectangle and Graphics.FillRoundedRectangle (System.Drawing.Common,
gated under NET9_0_OR_GREATER) currently build an arc-based GraphicsPath and stroke/fill it
with the default PixelOffsetMode and no anti-aliasing setup:
public void DrawRoundedRectangle(Pen pen, RectangleF rect, SizeF radius)
{
using GraphicsPath path = new();
path.AddRoundedRectangle(rect, radius);
DrawPath(pen, path);
}
Because the corner arcs are anti-aliased on a different sub-pixel grid than the crisp straight
edges, the rounded corners render with visible artifacts: the arcs look softer / offset relative
to the straight sides, and a centered stroke can be clipped when the rounded rectangle fills its
bounds.
Expected
Rounded-rectangle corners should blend seamlessly with the straight edges and produce a crisp,
uniform border and fill.
Repro
Draw a rounded rectangle with a modest corner radius and a border a few pixels thick and observe
the corner arcs versus the straight edges — the arcs appear blurrier / shifted.
Proposed fix
Apply a "high-quality inset stroke" technique inside the two methods:
- Set
SmoothingMode.AntiAlias and PixelOffsetMode.HighQuality (saved and restored around the
call so the caller's Graphics state is not permanently changed).
- For the stroke, inset the geometry by half the pen width so the entire border sits inside the
requested bounds on a consistent sub-pixel grid.
- Fill uses anti-aliasing + high-quality pixel offset (no inset needed).
This realigns the arc anti-aliasing with the straight edges and removes the artifact, while
keeping the public API signatures unchanged.
Problem
Graphics.DrawRoundedRectangleandGraphics.FillRoundedRectangle(System.Drawing.Common,gated under
NET9_0_OR_GREATER) currently build an arc-basedGraphicsPathand stroke/fill itwith the default
PixelOffsetModeand no anti-aliasing setup:Because the corner arcs are anti-aliased on a different sub-pixel grid than the crisp straight
edges, the rounded corners render with visible artifacts: the arcs look softer / offset relative
to the straight sides, and a centered stroke can be clipped when the rounded rectangle fills its
bounds.
Expected
Rounded-rectangle corners should blend seamlessly with the straight edges and produce a crisp,
uniform border and fill.
Repro
Draw a rounded rectangle with a modest corner radius and a border a few pixels thick and observe
the corner arcs versus the straight edges — the arcs appear blurrier / shifted.
Proposed fix
Apply a "high-quality inset stroke" technique inside the two methods:
SmoothingMode.AntiAliasandPixelOffsetMode.HighQuality(saved and restored around thecall so the caller's
Graphicsstate is not permanently changed).requested bounds on a consistent sub-pixel grid.
This realigns the arc anti-aliasing with the straight edges and removes the artifact, while
keeping the public API signatures unchanged.