Skip to content

System.Drawing: Graphics.DrawRoundedRectangle / FillRoundedRectangle produce anti-aliased corner artifacts #14804

Description

@KlausLoeffelmann

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions