dotnet / dotnet/winforms

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

Open
#14,804 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
1d 13m
Merged PRs (30d)
85

Description

### 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:

```csharp
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.

Contributor guide

Open the contributing guide

Research direction

Start with Graphics.DrawRoundedRectangle and Graphics.FillRoundedRectangle in System.Drawing.Common, gated under NET9_0_OR_GREATER, and inspect how they create and draw the GraphicsPath. Verify the saved and restored graphics state, anti-aliasing, pixel offset, and inset-stroke behavior against the reported corner artifacts. Done means rounded corners blend with straight edges while the centered stroke remains inside the requested bounds.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
computer-graphics, desktop
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.