dotnet/wpf · error · NotSupportedException

Cannot convert to type.

Error message

Cannot convert to type.

What it means

ImageSourceTypeConverter.ConvertTo only supports a fixed set of destination types (checked via IsSupportedType). When destinationType is not one of the supported targets (e.g. typeof(string) may not be among them), it throws NotSupportedException with Converter_ConvertToNotSupported ("Cannot convert to type.").

Solutions

  1. Check converter.CanConvertTo(context, destinationType) before calling ConvertTo and skip unsupported targets.
  2. Restrict calls to the destination types the converter supports (as used by the XPS serialization pipeline, e.g. resource Uri types).
  3. Catch NotSupportedException and provide a custom conversion (e.g. encode the BitmapSource manually) for the requested type.

Example fix

// before
var uri = converter.ConvertTo(context, culture, bitmapSource, typeof(string));

// after
if (converter.CanConvertTo(context, typeof(string)))
{
    var uri = converter.ConvertTo(context, culture, bitmapSource, typeof(string));
}
Defensive patterns

Strategy: validation

Validate before calling

if (!converter.CanConvertTo(context, destinationType))
{
    // skip or handle unsupported destination type before calling ConvertTo
    return null;
}

Try / catch

try
{
    result = converter.ConvertTo(context, culture, bitmapSource, destinationType);
}
catch (NotSupportedException ex)
{
    // encode manually, e.g. PngBitmapEncoder, for unsupported destination types
}

Prevention

When it happens

Trigger: Calling ConvertTo(context, culture, bitmapSource, destinationType) where destinationType is not in the converter's supported table — e.g. requesting typeof(string), typeof(byte[]), or another type the converter does not advertise via GetConvertTo / supported types.

Common situations: Serialization frameworks probing many destination types and ignoring CanConvertTo; designers converting image sources to strings for display; generic export utilities that assume universal converter support.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/ImageSourceTypeConverter.cs:153

        /// <returns>
        /// The Type to convert the value parameter to.
        /// </returns>
        public
        override
        object
        ConvertTo(
            ITypeDescriptorContext              context,
            System.Globalization.CultureInfo    culture,
            object                              value,
            Type                                destinationType
            )
        {
            Toolbox.EmitEvent(EventTrace.Event.WClientDRXConvertImageBegin);

            ArgumentNullException.ThrowIfNull(context);
            if (!IsSupportedType(destinationType))
            {
                throw new NotSupportedException(SR.Converter_ConvertToNotSupported);
            }

            //
            // Check that we have a valid BitmapSource instance.
            //
            BitmapSource bitmapSource = (BitmapSource)value;
            if (bitmapSource == null)
            {
                throw new ArgumentException(SR.Format(SR.MustBeOfType, "value", "BitmapSource"));
            }

            //
            // Get the current serialization manager.
            //
            PackageSerializationManager manager = (PackageSerializationManager)context.GetService(typeof(XpsSerializationManager));

            //Get the image Uri if it has already been serialized
            Uri imageUri = GetBitmapSourceFromImageTable(manager, bitmapSource);

View on GitHub (pinned to 81131a70a4)