Fix PrinterSettings.SupportsColor to use the right PInvoke (#34651)
authorSantiago Fernandez Madero <safern@microsoft.com>
Wed, 15 Apr 2020 01:10:28 +0000 (18:10 -0700)
committerGitHub <noreply@github.com>
Wed, 15 Apr 2020 01:10:28 +0000 (18:10 -0700)
* Fix PrinterSettings.SupportsColor to use the right PInvoke

* PR Fedback

* Fix test

* PR Feedbak

src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs
src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Windows.cs
src/libraries/System.Drawing.Common/tests/Printing/PrinterSettingsTests.cs

index e42459d..2a94494 100644 (file)
@@ -178,6 +178,7 @@ namespace System.Drawing
         DC_PAPERNAMES = 16,
         DC_ORIENTATION = 17,
         DC_COPIES = 18,
+        DC_COLORDEVICE = 32,
         PD_ALLPAGES = 0x00000000,
         PD_SELECTION = 0x00000001,
         PD_PAGENUMS = 0x00000002,
index 248ea74..2210000 100644 (file)
@@ -517,7 +517,12 @@ namespace System.Drawing.Printing
         {
             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;
             }
         }
 
index 096ad20..5839345 100644 (file)
@@ -431,12 +431,14 @@ namespace System.Drawing.Printing.Tests
             }
         }
 
-        [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 @@ namespace System.Drawing.Printing.Tests
 
         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",