Skip to content

Fixes PrintPreviewControl displays a black preview page after assigning an empty PrintDocument - #14857

Merged
ricardobossan merged 4 commits into
dotnet:mainfrom
ricardobossan:Issue_14838_PrintPreviewControl_displays_a_black_preview
Aug 14, 2026
Merged

ricardobossan merged 4 commits into
dotnet:mainfrom
ricardobossan:Issue_14838_PrintPreviewControl_displays_a_black_preview

Conversation

@ricardobossan

@ricardobossan ricardobossan commented Aug 6, 2026 •

Copy link
Copy Markdown
Member

Fixes #14838

Proposed changes

  • In PrintPreviewControl.DrawPages, the page-background fill now uses Color.White only when ForeColor was never explicitly set; an explicitly set ForeColor is still honored, as it has always been since the original .NET Framework port (uses the existing _isForeColorSet flag added by Fixes WinForms PrintPreviewControl ForeColor displays incorrectly only when set to White #14424).
  • Added unit tests covering both cases: default ForeColor renders white, explicit ForeColor renders that color.

Customer Impact

  • Fixes the solid-black preview page reported for a PrintDocument that produces no printable content, with ForeColor left at its default.
  • Apps that explicitly set ForeColor to customize the page background keep working exactly as before; no behavior change for them.

Regression?

Risk

Screenshots

Before

before_fix

After

after_fix

Test methodology

  • Unit tests

Test environment(s)

  • 11.0.100-preview.6.26359.118
Microsoft Reviewers: Open in CodeFlow

…ng an empty PrintDocument

Fixes dotnet#14838

## Proposed changes

- In `PrintPreviewControl.DrawPages`, fill each page rectangle with a
fixed `Color.White` "paper" background instead of the control's
`ForeColor`.
- Added a regression unit test asserting a page with no drawable content
renders white, not `ForeColor`.

## Customer Impact

- Print preview pages render as white paper again, regardless of the
control's `ForeColor`.
- Fixes the solid-black preview page reported for a `PrintDocument` that
produces no printable content, and removes the general risk of the page
background matching a dark `ForeColor`.

## Regression?

- Yes. Regression from dotnet#14424 (merged 2026-07-02), which correctly
changed `ResetForeColor()`'s default from `Color.White` to
`SystemColors.ControlText` to fix dotnet#14420. `DrawPages()` separately
reused `ForeColor` to fill each page's background (present since the
original .NET Framework port) and only rendered white by coincidence,
because the old default happened to match. This PR doesn't touch
dotnet#14424's changes (`ForeColor`, `ResetForeColor`,
`ShouldSerializeForeColor`, `DrawMessage`), so dotnet#14420 stays fixed; it
only changes the unrelated page-background fill in `DrawPages`.

## Risk

- Low. Single fill-color change, scoped to the page-background rectangle
in `DrawPages`. Verified it doesn't touch `ForeColor`, `ResetForeColor`,
`ShouldSerializeForeColor`, `_isForeColorSet`, or `DrawMessage` (the
members dotnet#14424 added/changed for dotnet#14420).
- The white fill isn't theme-aware, matching how this code behaved from
the original .NET Framework port through dotnet#14424 (`Color.White`). No
issue, test, or doc comment in this control's history ties `ForeColor`
to page-background color (dotnet#13861/dotnet#13863/dotnet#14420/dotnet#14424 are all
specifically about the "no printable content" message text), so this
restores the long-standing default rather than removing a documented
capability. If someone was relying on `ForeColor` to tint the blank-page
fill as a side effect, that (undocumented) behavior is gone; open to
making it configurable separately if that's a real use case.

## Screenshots

### Before

### After

## Test methodology

- Added `PrintPreviewControl_PageWithNoImage_RendersWhiteNotForeColor`:
builds a `PrintPreviewControl` with `ForeColor = Color.Black` and
injects a synthetic page with no image (`new PreviewPageInfo(image:
null, physicalSize: ...)`, standing in for an empty `PrintDocument`'s
output), renders to a bitmap, and asserts the page interior is white
rather than black.
- Confirmed the new test fails against the pre-fix code (reproducing the
reported black page) and passes after the fix, to make sure it actually
exercises the regression.
- Ran the full `PrintPreviewControlTests` class locally: 8/8 passing.
- Manually reproduced the issue end-to-end in `ScratchProject`: reverted
the fix, rebuilt, confirmed the solid black page shown above, then
restored the fix, rebuilt, and confirmed the page renders white
(screenshots above).

## Test environment(s)

- 11.0.100-preview.6.26359.118
@ricardobossan ricardobossan self-assigned this Aug 6, 2026
@ricardobossan
ricardobossan requested a review from a team as a code owner August 6, 2026 01:14
@ricardobossan ricardobossan added the waiting-review This item is waiting on review by one or more members of team label Aug 6, 2026
SimonZhao888
SimonZhao888 previously approved these changes Aug 6, 2026
@SimonZhao888

Copy link
Copy Markdown
Member

Looks good to me!

@dotnet-policy-service dotnet-policy-service Bot added the waiting-author-feedback The team requires more information from the author label Aug 6, 2026
@dotnet-policy-service dotnet-policy-service Bot removed the waiting-author-feedback The team requires more information from the author label Aug 7, 2026
@ricardobossan
ricardobossan requested a review from LeafShi1 August 7, 2026 01:30
@LeafShi1

LeafShi1 commented Aug 7, 2026 •

Copy link
Copy Markdown
Member

We have made several rounds of changes regarding the color issue with PrintPreviewControl. However, I feel that if we continue to use ForeColor to control both the message and the PrintDocument within the PrintPreviewControl, it will be difficult to achieve a satisfactory result for both elements.

I have two ideas:

  1. Revisit the original issue ([Accessibility] The text "Document does not contain any pages" in printPreviewControl has low color contrast #12365). Instead of changing the message color, we could change the PrintPreviewControl background color to a darker gray. I’ve noticed that the contrast is insufficient not only between the message and the control's background but also between the control's background and the form itself. I discussed this with Olina, and we consider it a valid issue. I’m wondering if using a darker gray for the PrintPreviewControl background might solve both problems simultaneously—though I acknowledge this would be a breaking change.

    image
  2. If we set aside the contrast issue between the PrintPreviewControl background and the form, could we add a separate property for PrintDocument—such as PageBackColor—to control the document's background color, while using the PrintPreviewControl's ForeColor solely to control the message color? This might be a more suitable approach.

What are your thoughts on this? @ricardobossan @KlausLoeffelmann

@KlausLoeffelmann

Copy link
Copy Markdown
Member

I am wondering, what .NET 11 can do to regress this?
I would rather think .NET 9/.NET 10 might regress this, once you bring DarkMode into play. Please keep this in mind - I forget this myself once in a while, and I wrote this originally! 😸

SystemColors.UseAlternativeColorSet = darkModeEnabled;

When we're switching to DarkMode, we're changing the SystemColors palette to its complementary colors with that line (well, they are and cannot be exactly complementary - but, you know what I mean, principally). And then from that moment on, also for the PrintPreviewControl you would assign complementary colors. Is that what is happening here, because this is one of the cases where that MUST not happening.

In so far, I am actually of the same opinion as @LeafShi1 - we should figure out a dedicated color combination which would work in every mode with reasonable contrast. Reasonable enough, that @Olina-Zhang allows us to keep the color combination - because she is the boss for color-combination that work sufficiently with A11Y! 😺

@ricardobossan

Copy link
Copy Markdown
Member Author

I am wondering, what .NET 11 can do to regress this? I would rather think .NET 9/.NET 10 might regress this, once you bring DarkMode into play. Please keep this in mind - I forget this myself once in a while, and I wrote this originally! 😸

SystemColors.UseAlternativeColorSet = darkModeEnabled;

When we're switching to DarkMode, we're changing the SystemColors palette to its complementary colors with that line (well, they are and cannot be exactly complementary - but, you know what I mean, principally). And then from that moment on, also for the PrintPreviewControl you would assign complementary colors. Is that what is happening here, because this is one of the cases where that MUST not happening.

In so far, I am actually of the same opinion as @LeafShi1 - we should figure out a dedicated color combination which would work in every mode with reasonable contrast. Reasonable enough, that @Olina-Zhang allows us to keep the color combination - because she is the boss for color-combination that work sufficiently with A11Y! 😺

Status update, and a finding that's bigger than this PR.

#14838 is fixed. ForeColor again drives the page background when explicitly set, matching behavior since .NET Framework. What changed is how "explicitly set" is tracked: ForeColor's setter is now overridden directly instead of relying on ForeColorChanged (which only fires on an actual value change, the bug in @LeafShi1 's second comment). So reassigning SystemColors.ControlText when that's already the value, her exact repro, now correctly counts as explicit. Verified this doesn't false-positive on ambient/inherited ForeColor: OnParentForeColorChanged bypasses the property setter entirely (confirmed from Control.cs).

Net effect: default ForeColor renders white instead of black (the reported bug); an explicit ForeColor still drives the page, exactly like every prior release. Not a breaking change.

#14420 stays fixed too; ResetForeColor()/ShouldSerializeForeColor()/DrawMessage() are untouched from the original PR.

#12365 (background contrast) is separate, harder, and worse than reported. Tried five BackColor candidates (AppWorkspace (current), ControlDarkDark, WindowFrame, ControlDark, GrayText): computed WCAG ratios in Classic/Dark, then live testing across 4 Windows 11 Contrast themes (Aquatic, Desert, Dusk, Night sky) plus Accessibility Insights. None holds up:

  • ControlDarkDark/WindowFrame: collapse to the same color as the page in 3 of 4 themes.
  • ControlDark: fails contrast in plain Light/Classic mode, no HC involved.
  • GrayText: passes Accessibility Insights only in "None" (no HC) and Desert; fails the other three.
  • AppWorkspace, shipping today: same three themes render white-on-white, background and page identical. Not introduced by this PR, already live in production.

In every failing case the page's 1px black border (a fixed literal, unaffected by theme) still marks where the page is, so it degrades rather than breaks usability. Genuinely unsure if that's an accepted tradeoff already or a real gap; if this is known behavior, ignore the rest of this paragraph.

None of AppWorkspace, ControlDarkDark, WindowFrame, ControlDark, or GrayText gets this right, so guessing through more SystemColors tokens doesn't seem productive. Options as I see them:

  1. Leave BackColor alone here (already the case) and track this on #12365 (or a new issue, happy to open one if preferred) for whenever there's bandwidth to design an actual combination; cc @Olina-Zhang per @KlausLoeffelmann 's note that this needs her sign-off.
  2. Something else, if either of you sees an approach those five tokens don't cover.
    Either way, #14838 itself is ready independent of how #12365 gets resolved. Let me know which you'd prefer.

@KlausLoeffelmann

Copy link
Copy Markdown
Member

Correct me if I am wrong - but the background will never carry any text - or does it?
Which means the usual Text contrast rules hardly apply here, and you can clearly see, where the page borders are, I assume, no matter the theming.

We can ignore that - since this is no contrast issue in that case.
If there is nothing else - let's take this!

@LeafShi1

Copy link
Copy Markdown
Member

Correct me if I am wrong - but the background will never carry any text - or does it? Which means the usual Text contrast rules hardly apply here, and you can clearly see, where the page borders are, I assume, no matter the theming.

We can ignore that - since this is no contrast issue in that case. If there is nothing else - let's take this!

It's not entirely devoid of text here, as an empty document would display "Document does not contain any pages".

However, I agree that this seems unrelated to #14838 and should probably be discussed as #12365 or a separate issue.

@KlausLoeffelmann KlausLoeffelmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should take this now, to fix the main issue.
If we need to revisit it, should A11Y contrast remain an issue in certain scenarios, we address that in follow-up PRs.

@KlausLoeffelmann KlausLoeffelmann removed the waiting-review This item is waiting on review by one or more members of team label Aug 14, 2026
@ricardobossan
ricardobossan merged commit c522de4 into dotnet:main Aug 14, 2026
8 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Aug 15, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

4 participants