From 4265a130461fe75ba71b2c125cf1b00ab4ae2145 Mon Sep 17 00:00:00 2001 From: Santiago Fernandez Madero Date: Tue, 14 Apr 2020 18:10:28 -0700 Subject: [PATCH] Fix PrinterSettings.SupportsColor to use the right PInvoke (#34651) * Fix PrinterSettings.SupportsColor to use the right PInvoke * PR Fedback * Fix test * PR Feedbak --- .../src/System/Drawing/Gdiplus.cs | 1 + .../Drawing/Printing/PrinterSettings.Windows.cs | 7 ++++++- .../tests/Printing/PrinterSettingsTests.cs | 12 +++++++----- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs index e42459df59d1b..2a9449435a938 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs @@ -178,6 +178,7 @@ public const int DC_PAPERNAMES = 16, DC_ORIENTATION = 17, DC_COPIES = 18, + DC_COLORDEVICE = 32, PD_ALLPAGES = 0x00000000, PD_SELECTION = 0x00000001, PD_PAGENUMS = 0x00000002, diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Windows.cs index 248ea74736119..2210000175fad 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Windows.cs @@ -517,7 +517,12 @@ public bool SupportsColor { get { - return GetDeviceCaps(Interop.Gdi32.DeviceCapability.BITSPIXEL, 1) > 1; + // If the printer supports color printing, the return value is 1; otherwise, the return value is zero. + // The pointerToBuffer parameter is not used. + return DeviceCapabilities( + capability: SafeNativeMethods.DC_COLORDEVICE, + pointerToBuffer: IntPtr.Zero, + defaultValue: 0) == 1; } } diff --git a/src/libraries/System.Drawing.Common/tests/Printing/PrinterSettingsTests.cs b/src/libraries/System.Drawing.Common/tests/Printing/PrinterSettingsTests.cs index 096ad2001e8d0..58393454f5425 100644 --- a/src/libraries/System.Drawing.Common/tests/Printing/PrinterSettingsTests.cs +++ b/src/libraries/System.Drawing.Common/tests/Printing/PrinterSettingsTests.cs @@ -431,12 +431,14 @@ public void IsDirectPrintingSupported_ImageNotSupported_ReturnsExpected() } } - [ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)] - [ConditionalFact(Helpers.AnyInstalledPrinters, Helpers.IsDrawingSupported)] + [PlatformSpecific(TestPlatforms.Windows)] + [ConditionalFact(typeof(PrinterSettingsTests), nameof(CanTestSetHdevmode_IntPtr_Success))] public void SupportsColor_ReturnsExpected() { - var printerSettings = new PrinterSettings(); - bool supportsColor = printerSettings.SupportsColor; + // XPS and PDF printers support color. + // docs.microsoft.com/en-us/windows-hardware/drivers/print/improved-color-printing + var printerSettings = new PrinterSettings() { PrinterName = GetNameOfTestPrinterSuitableForDevModeTesting() }; + Assert.True(printerSettings.SupportsColor); } [Theory] @@ -608,7 +610,7 @@ private static string GetNameOfTestPrinterSuitableForDevModeTesting() private static readonly string[] s_TestPrinterNames = { - // Our method of testing this api requires a printer that supports multi-copy printing, collating and duplex settings. Not all printers + // Our method of testing some apis requires a printer that supports multi-copy printing, collating, color and duplex settings. Not all printers // support these so rather than trust the machine running the test to have configured such a printer as the default, use the name of // a known compliant printer that ships with Windows 10. "Microsoft Print to PDF",