diff --git a/src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs b/src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs index 342ff10af83..9e596257693 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs @@ -690,7 +690,11 @@ private void DrawPages(Graphics g, Rectangle rect, PreviewPageInfo[] pages, Brus { Rectangle box = pageRenderArea[i]; g.DrawRectangle(Pens.Black, box); - using (var brush = ForeColor.GetCachedSolidBrushScope()) + + // Default page fill is white (paper); an explicitly set ForeColor is still honored, + // as it always has been. + Color pageColor = _isForeColorSet ? ForeColor : Color.White; + using (var brush = pageColor.GetCachedSolidBrushScope()) { g.FillRectangle(brush, box); } diff --git a/src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs b/src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs index eaaf19a51fc..6788b199e0e 100644 --- a/src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs +++ b/src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs @@ -4,6 +4,7 @@ #nullable disable using System.Drawing; +using System.Drawing.Printing; namespace System.Windows.Forms.Tests; @@ -68,6 +69,57 @@ public void PrintPreviewControl_ForeColorReset_ShouldSerializeReturnsFalse() Assert.Equal(SystemColors.ControlText.ToArgb(), control.ForeColor.ToArgb()); } + [WinFormsFact] + public void PrintPreviewControl_PageWithNoImage_DefaultForeColor_RendersWhite() + { + // Regression test for #14838: a page with no drawable content (e.g. from an empty + // PrintDocument), with ForeColor left at its default, must render as white paper, + // not as a solid black rectangle. + using PrintPreviewControl control = new() + { + Size = new Size(200, 200) + }; + + control.CreateControl(); + + PreviewPageInfo[] pageInfo = [new(image: null, physicalSize: new Size(850, 1100))]; + control.TestAccessor.Dynamic._pageInfo = pageInfo; + + using Bitmap bitmap = new(control.Width, control.Height); + control.DrawToBitmap(bitmap, new Rectangle(Point.Empty, control.Size)); + + // The single page fills nearly the whole control, so the center pixel lands well + // inside the page interior, away from its 1px black border. + Color centerPixel = bitmap.GetPixel(bitmap.Width / 2, bitmap.Height / 2); + + Assert.Equal(Color.White.ToArgb(), centerPixel.ToArgb()); + } + + [WinFormsFact] + public void PrintPreviewControl_PageWithNoImage_ExplicitForeColor_RendersForeColor() + { + // ForeColor has driven the page background fill since the original .NET Framework port; + // an explicitly set value must still be honored (see PR #14857 discussion), not overridden + // by the white default that only applies when ForeColor was never set. + using PrintPreviewControl control = new() + { + ForeColor = Color.Red, + Size = new Size(200, 200) + }; + + control.CreateControl(); + + PreviewPageInfo[] pageInfo = [new(image: null, physicalSize: new Size(850, 1100))]; + control.TestAccessor.Dynamic._pageInfo = pageInfo; + + using Bitmap bitmap = new(control.Width, control.Height); + control.DrawToBitmap(bitmap, new Rectangle(Point.Empty, control.Size)); + + Color centerPixel = bitmap.GetPixel(bitmap.Width / 2, bitmap.Height / 2); + + Assert.Equal(Color.Red.ToArgb(), centerPixel.ToArgb()); + } + [Fact] public void ShowPrintPreviewControlHighContrast_BackColorIsCorrect() {