dotnet / dotnet/dotnet-api-docs

PrinterSettings.SupportsColor property is broken

Aperta
#4,083 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
area-System.Drawing Pri3 untriaged
Lingua principale
C#
Stelle
949
Fork
1.7k
Merge medio
2g 19h
PR unite (30g)
52

Descrizione

[Documentation](https://docs.microsoft.com/en-us/dotnet/api/system.drawing.printing.printersettings.supportscolor?view=netframework-4.8) claims that `SupportsColor` property :

> Gets a value indicating whether this printer supports color printing.

This is not accurate. This [property](https://github.com/dotnet/runtime/blob/1cf033de96a38b286c07b8311edd5f2eabcfb462/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Windows.cs#L516-L522) P/Invokes [GetDeviceCaps(BITSPIXEL..)](https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getdevicecaps), which returns:

> Number of adjacent color bits for each pixel.

It does not take into account "color planes" or gray-scale rendering. I would not recommend using this API in .NET Framework. However in .NET Core 5, we fixed it(issue #34630). I think we should publish a workaround in the docs:

```
using System;
using System.Drawing.Printing;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] _)
{
PrinterSettingsTest();
}

static void PrinterSettingsTest()
{
var document = new PrintDocument();
foreach (string name in PrinterSettings.InstalledPrinters)
{
document.PrinterSettings.PrinterName = name;
Console.WriteLine($"{name} SupportsColor = {document.PrinterSettings.SupportsColor}");
var capability = GetColorDeviceCapability(name);
Console.WriteLine($"\tDeviceCapabilities(DC_COLORDEVICE) returns {capability}");
}

Console.ReadLine();
}

[DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true)]
private static extern int DeviceCapabilitiesW(string pDevice, string pPort, short fwCapabilities, IntPtr pOutput, IntPtr pDevMode);
private const short DC_COLORDEVICE = 32;

private static bool GetColorDeviceCapability(string printerName)
{
return DeviceCapabilitiesW(printerName, null, DC_COLORDEVICE, IntPtr.Zero, IntPtr.Zero) == 1;
}
}
}
```

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Start with the PrinterSettings.SupportsColor API documentation linked in the issue, then compare its description with PrinterSettings.Windows.cs and the referenced GetDeviceCaps behavior. Document the .NET Framework limitation and the supplied DeviceCapabilitiesW workaround, with the page clearly distinguishing the corrected .NET Core 5 behavior.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.