-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix rounded-rectangle rendering artifacts with high-quality inset stroke #14805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KlausLoeffelmann
wants to merge
1
commit into
dotnet:feature/11.0
Choose a base branch
from
KlausLoeffelmann:klaus/fix-rounded-rectangle-artifacts
base: feature/11.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+93
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -730,9 +730,41 @@ public void DrawRoundedRectangle(Pen pen, Rectangle rect, Size radius) => | |
| /// <param name="radius">The radius width and height used to round the corners of the rectangle.</param> | ||
| public void DrawRoundedRectangle(Pen pen, RectangleF rect, SizeF radius) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(pen); | ||
|
|
||
| // Inset the stroke by half the pen width so the entire border sits inside the requested | ||
| // bounds on a consistent sub-pixel grid. Combined with high-quality pixel offset and | ||
| // anti-aliasing this realigns the corner arcs with the straight edges and removes the | ||
| // artifacts a centered, default-offset stroke would otherwise produce. | ||
| RectangleF strokeRect = rect; | ||
| SizeF strokeRadius = radius; | ||
|
|
||
| if (rect.Width > pen.Width && rect.Height > pen.Width) | ||
| { | ||
| float inset = pen.Width / 2f; | ||
| strokeRect = RectangleF.Inflate(rect, -inset, -inset); | ||
| strokeRadius = new( | ||
| Math.Max(0f, radius.Width - inset), | ||
| Math.Max(0f, radius.Height - inset)); | ||
| } | ||
|
|
||
| using GraphicsPath path = new(); | ||
| path.AddRoundedRectangle(rect, radius); | ||
| DrawPath(pen, path); | ||
| path.AddRoundedRectangle(strokeRect, strokeRadius); | ||
|
|
||
| Drawing2D.SmoothingMode previousSmoothingMode = this.SmoothingMode; | ||
| PixelOffsetMode previousPixelOffsetMode = this.PixelOffsetMode; | ||
|
|
||
| try | ||
| { | ||
| this.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias; | ||
| this.PixelOffsetMode = PixelOffsetMode.HighQuality; | ||
| DrawPath(pen, path); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "this" can be removed |
||
| } | ||
| finally | ||
| { | ||
| this.SmoothingMode = previousSmoothingMode; | ||
| this.PixelOffsetMode = previousPixelOffsetMode; | ||
| } | ||
| } | ||
| #endif | ||
|
|
||
|
|
@@ -1204,9 +1236,28 @@ public void FillRoundedRectangle(Brush brush, Rectangle rect, Size radius) => | |
| /// <param name="radius">The radius width and height used to round the corners of the rectangle.</param> | ||
| public void FillRoundedRectangle(Brush brush, RectangleF rect, SizeF radius) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(brush); | ||
|
|
||
| using GraphicsPath path = new(); | ||
| path.AddRoundedRectangle(rect, radius); | ||
| FillPath(brush, path); | ||
|
|
||
| // Anti-aliasing plus high-quality pixel offset gives the filled corners the same crisp, | ||
| // uniform edge as the straight sides instead of the softer, offset arcs the default | ||
| // rendering mode produces. | ||
| Drawing2D.SmoothingMode previousSmoothingMode = this.SmoothingMode; | ||
| PixelOffsetMode previousPixelOffsetMode = this.PixelOffsetMode; | ||
|
|
||
| try | ||
| { | ||
| this.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias; | ||
| this.PixelOffsetMode = PixelOffsetMode.HighQuality; | ||
| FillPath(brush, path); | ||
| } | ||
| finally | ||
| { | ||
| this.SmoothingMode = previousSmoothingMode; | ||
| this.PixelOffsetMode = previousPixelOffsetMode; | ||
| } | ||
| } | ||
| #endif | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can drop the inset logic entirely and keep just the
AntiAlias + PixelOffsetMode.HighQualitychange.rect > pen.Widthguard is a hard threshold, so geometry jumps discontinuously as size crosses it. With a 10px pen, a 12×12 rect insets down to a 2×2 path (radius 3) → visibly malformed next to the 8×8/10×10 ones that aren't inset at all.DrawRectangle. Every otherDraw*centers the pen on the bounds, insetting makesDrawRoundedRectanglethe odd one out, so mixing it withDrawRectangleat the same bounds no longer lines up.