dotnet/wpf · error · ArgumentException

SR.Format(SR.General_Expected_Type, nameof(PixelFormat))

Error message

SR.Format(SR.General_Expected_Type, nameof(PixelFormat))

What it means

PixelTypeConverter.ConvertTo requires the value being converted to be a PixelFormat instance. If value is null or not a PixelFormat, it throws ArgumentException with General_Expected_Type naming PixelFormat. Type converters are used by designers/serialization; passing the wrong object type is rejected with this message.

Solutions

  1. Pass an actual PixelFormat instance (e.g. PixelFormats.Bgr32) to ConvertTo; parse strings via new PixelFormat(s) or TypeDescriptor first
  2. Check value is PixelFormat before invoking the converter
  3. If converting from string, call ConvertFrom instead of ConvertTo
  4. Handle ArgumentException at the serialization boundary and log/report the offending type

Example fix

// before
converter.ConvertTo(ctx, culture, "Bgr32", typeof(InstanceDescriptor)); // throws
// after
object val = value is string s ? new PixelFormat(s) : value;
converter.ConvertTo(ctx, culture, val, typeof(InstanceDescriptor));
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not PixelFormat pf) throw new ArgumentException($"Expected PixelFormat, got {value?.GetType().Name ?? "null"}", nameof(value));

Type guard

bool IsPixelFormat(object v) => v is PixelFormat;

Try / catch

try { return converter.ConvertTo(ctx, culture, value, destinationType); }
catch (ArgumentException ex) { log(ex); return value?.ToString(); }

Prevention

When it happens

Trigger: Calling PixelFormatConverter.ConvertTo(context, culture, value, destinationType) with a value that is not a PixelFormat (e.g. a string like "Bgr32", a PixelFormats sentinel wrapper, or null) — commonly from designer/property-grid serialization pipelines.

Common situations: Custom designer code passing a string instead of a PixelFormat instance; binding or reflection code feeding untyped property values into ConvertTo; serialization frameworks invoking the converter with raw config values.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/PixelFormatConverter.cs:78

        {
            return o is not null ? new PixelFormat(o as string) : null;
        }

        /// <summary>
        /// Converts a <paramref name="value"/> of <see cref="PixelFormat"/> to the specified <paramref name="destinationType"/>.
        /// </summary>
        /// <param name="context">Context information used for conversion.</param>
        /// <param name="culture">The culture specifier to use.</param>
        /// <param name="value"><see cref="PixelFormat"/> value to convert from.</param>
        /// <param name="destinationType">Type being evaluated for conversion.</param>
        /// <returns>A <see cref="string"/> or <see cref="InstanceDescriptor"/> representing the <see cref="PixelFormat"/> specified by <paramref name="value"/>.</returns>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            ArgumentNullException.ThrowIfNull(destinationType);
            ArgumentNullException.ThrowIfNull(value);

            if (value is not PixelFormat pixelFormat)
                throw new ArgumentException(SR.Format(SR.General_Expected_Type, nameof(PixelFormat)));

            if (destinationType == typeof(InstanceDescriptor))
            {
                ConstructorInfo ci = typeof(PixelFormat).GetConstructor(new Type[] { typeof(string) });
                return new InstanceDescriptor(ci, new object[] { pixelFormat.ToString() });
            }
            else if (destinationType == typeof(string))
            {
                return pixelFormat.ToString();
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}

View on GitHub (pinned to 81131a70a4)