dotnet/wpf · error · SystemException

Unable to create a device context from the specified device…

Error message

Unable to create a device context from the specified device information.

What it means

CreateDC wraps the Win32 CreateDC call in a SafeDC handle. If the returned device context is invalid (native CreateDC failed, e.g. unresolvable device name or GDI failure), the SafeDC is disposed and a SystemException is thrown, since no rendering target can be created from the given device information.

Solutions

  1. Verify the device name/DEVMODE is current — re-enumerate printers via PrinterSettings.InstalledPrinters before creating the DC.
  2. Catch SystemException and fall back to a default device context (null device name = screen DC).
  3. Re-select the default printer or prompt the user to pick a valid device.
  4. Update/reinstall the device driver; test the device works outside the app.

Example fix

// before
var dc = NativeMethods.CreateDC(deviceName, null, null, devMode); // SystemException if invalid
// after
if (PrinterSettings.InstalledPrinters.Contains(deviceName))
    var dc = NativeMethods.CreateDC(deviceName, null, null, devMode);
else
    deviceName = fallbackPrinter;
Defensive patterns

Strategy: try-catch

Validate before calling

if (string.IsNullOrEmpty(deviceName) ||
    !PrinterSettings.InstalledPrinters.Cast<string>().Contains(deviceName))
    throw new ArgumentException("Device not installed: " + deviceName);

Try / catch

try { dc = NativeMethods.CreateDC(deviceName, null, null, devMode); }
catch (SystemException ex)
{
    Log.Error("CreateDC failed for " + deviceName, ex);
    dc = NativeMethods.CreateDC(null, null, null, null); // screen DC fallback
}

Prevention

When it happens

Trigger: Passing device information that GDI cannot resolve — wrong/renamed printer or device name, disconnected device, bad DEVMODE, driver failure — so native CreateDC returns NULL.

Common situations: Printing to a printer that was removed or renamed; building device contexts from stale DEVMODE data; driver corruption after Windows updates; running in sessions without access to the target device.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/4fac91db7c4adf51. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Standard/NativeMethods.cs:1435

        [SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes")]
        public static SafeDC CreateDC(string deviceName)
        {
            SafeDC dc = null;
            try
            {
                // Should this really be on the driver parameter?
                dc = NativeMethods.CreateDC(deviceName, null, IntPtr.Zero, IntPtr.Zero);
            }
            finally
            {
                dc?._created = true;
            }

            if (dc.IsInvalid)
            {
                dc.Dispose();
                throw new SystemException("Unable to create a device context from the specified device information.");
            }

            return dc;
        }

        [SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes")]
        public static SafeDC CreateCompatibleDC(SafeDC hdc)
        {
            SafeDC dc = null;
            try
            {
                IntPtr hPtr = IntPtr.Zero;
                if (hdc != null)
                {
                    hPtr = hdc.handle;
                }
                dc = NativeMethods.CreateCompatibleDC(hPtr);
                if (dc == null)

View on GitHub (pinned to 81131a70a4)