dotnet/wpf · error · ArgumentException

General_Expected_Type (ImageSource)

Error message

General_Expected_Type (ImageSource)

What it means

ImageSourceConverter.CanConvertTo throws ArgumentException with 'General_Expected_Type' (expecting ImageSource) when the conversion context's Instance is not an ImageSource. The converter only supports serializing instances that are ImageSources, so a foreign context instance is rejected as an invalid argument.

Solutions

  1. Ensure the converter is only used on ImageSource instances (e.g. BitmapImage, DrawingImage)
  2. Pass a correct ITypeDescriptorContext whose Instance is the ImageSource
  3. Check `context.Instance is ImageSource` before invoking the converter

Example fix

// before
converter.CanConvertTo(badContext, typeof(string)); // context.Instance is not ImageSource
// after
if (badContext?.Instance is ImageSource)
{
    converter.CanConvertTo(badContext, typeof(string));
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (context?.Instance is not ImageSource)
    throw new ArgumentException("Converter context instance must be an ImageSource", nameof(context));

Type guard

bool IsImageSourceContext(ITypeDescriptorContext ctx) => ctx?.Instance is ImageSource;

Try / catch

try { return converter.CanConvertTo(context, typeof(string)); }
catch (ArgumentException) { return false; /* not an ImageSource instance */ }

Prevention

When it happens

Trigger: TypeConverter infrastructure invoking CanConvertTo/serialization on an ImageSourceConverter with an ITypeDescriptorContext whose Instance is not an ImageSource — typically when the converter is mistakenly associated with or invoked for a non-ImageSource object.

Common situations: Custom XAML serialization or designer tooling that passes the wrong context instance; misuse of ImageSourceConverter on a property whose declaring object is not an ImageSource.

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/181b24138f91f8e1. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ImageSourceConverter.cs:55

        /// <summary>
        /// Returns true if this type converter can convert to the given type.
        /// </summary>
        /// <returns>
        /// bool - True if this converter can convert to the provided type, false if not.
        /// </returns>
        /// <param name="context"> The ITypeDescriptorContext for this call. </param>
        /// <param name="destinationType"> The Type being queried for support. </param>
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                // When invoked by the serialization engine we can convert to string only for some instances
                if (context != null && context.Instance != null)
                {
                    if (!(context.Instance is ImageSource))
                    {
                        throw new ArgumentException(SR.Format(SR.General_Expected_Type, "ImageSource"), nameof(context));
                    }

                    ImageSource value = (ImageSource)context.Instance;

                    return value.CanSerializeToString();
                }

                return true;
            }

            return base.CanConvertTo(context, destinationType);
        }

        /// <summary>
        /// Attempts to convert to a ImageSource from the given object.
        /// </summary>
        /// <returns>
        /// The ImageSource which was constructed.

View on GitHub (pinned to 81131a70a4)