{"record":{"id":"95024ec81331d661","repo":"dotnet/wpf","slug":"cannot-convert-to-type-imagesourcetypeconverter","errorCode":null,"errorMessage":"Cannot convert to type.","messagePattern":"Cannot convert to type\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/ImageSourceTypeConverter.cs","lineNumber":153,"sourceCode":"        /// <returns>\n        /// The Type to convert the value parameter to.\n        /// </returns>\n        public\n        override\n        object\n        ConvertTo(\n            ITypeDescriptorContext              context,\n            System.Globalization.CultureInfo    culture,\n            object                              value,\n            Type                                destinationType\n            )\n        {\n            Toolbox.EmitEvent(EventTrace.Event.WClientDRXConvertImageBegin);\n\n            ArgumentNullException.ThrowIfNull(context);\n            if (!IsSupportedType(destinationType))\n            {\n                throw new NotSupportedException(SR.Converter_ConvertToNotSupported);\n            }\n\n            //\n            // Check that we have a valid BitmapSource instance.\n            //\n            BitmapSource bitmapSource = (BitmapSource)value;\n            if (bitmapSource == null)\n            {\n                throw new ArgumentException(SR.Format(SR.MustBeOfType, \"value\", \"BitmapSource\"));\n            }\n\n            //\n            // Get the current serialization manager.\n            //\n            PackageSerializationManager manager = (PackageSerializationManager)context.GetService(typeof(XpsSerializationManager));\n\n            //Get the image Uri if it has already been serialized\n            Uri imageUri = GetBitmapSourceFromImageTable(manager, bitmapSource);","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/ImageSourceTypeConverter.cs#L135-L171","documentation":"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.\").","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check converter.CanConvertTo(context, destinationType) before calling ConvertTo and skip unsupported targets.","Restrict calls to the destination types the converter supports (as used by the XPS serialization pipeline, e.g. resource Uri types).","Catch NotSupportedException and provide a custom conversion (e.g. encode the BitmapSource manually) for the requested type."],"exampleFix":"// before\nvar uri = converter.ConvertTo(context, culture, bitmapSource, typeof(string));\n\n// after\nif (converter.CanConvertTo(context, typeof(string)))\n{\n    var uri = converter.ConvertTo(context, culture, bitmapSource, typeof(string));\n}","handlingStrategy":"validation","validationCode":"if (!converter.CanConvertTo(context, destinationType))\n{\n    // skip or handle unsupported destination type before calling ConvertTo\n    return null;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    result = converter.ConvertTo(context, culture, bitmapSource, destinationType);\n}\ncatch (NotSupportedException ex)\n{\n    // encode manually, e.g. PngBitmapEncoder, for unsupported destination types\n}","preventionTips":["Always call CanConvertTo before ConvertTo.","Only request destination types used by the XPS pipeline.","Enumerate supported types once and cache the result rather than probing repeatedly."],"tags":["wpf","xps","typeconverter","unsupported-conversion"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}