{"record":{"id":"69eb9780a2f1c9bc","repo":"dotnet/wpf","slug":"converter-converttonotsupported","errorCode":null,"errorMessage":"Converter_ConvertToNotSupported","messagePattern":"Converter_ConvertToNotSupported","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ImageSourceConverter.cs","lineNumber":197,"sourceCode":"        /// </exception>\n        /// <param name=\"context\"> The ITypeDescriptorContext for this call. </param>\n        /// <param name=\"culture\"> The CultureInfo which is respected when converting. </param>\n        /// <param name=\"value\"> The object to convert to an instance of \"destinationType\". </param>\n        /// <param name=\"destinationType\"> The type to which this will convert the ImageSource instance. </param>\n        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)\n        {\n            if (destinationType != null && value is ImageSource)\n            {\n                ImageSource instance = (ImageSource)value;\n\n                if (destinationType == typeof(string))\n                {\n                    // When invoked by the serialization engine we can convert to string only for some instances\n                    if (context != null && context.Instance != null)\n                    {\n                        if (!instance.CanSerializeToString())\n                        {\n                            throw new NotSupportedException(SR.Converter_ConvertToNotSupported);\n                        }\n                    }\n\n                    // Delegate to the formatting/culture-aware ConvertToString method.\n                    return instance.ConvertToString(null, culture);\n                }\n            }\n\n            // Pass unhandled cases to base class (which will throw exceptions for null value or destinationType.)\n            return base.ConvertTo(context, culture, value, destinationType);\n        }\n\n        /// Try to get a bitmap out of a byte array.  This is an ole format that Access uses.\n        /// this fails very quickly so we can try this first without a big perf hit.\n        private unsafe Stream GetBitmapStream(byte[] rawData)\n        {\n            Debug.Assert(rawData != null, \"rawData is null.\");\n            fixed (byte* pByte = rawData)","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ImageSourceConverter.cs#L179-L215","documentation":"ImageSourceConverter.ConvertTo throws NotSupportedException with 'Converter_ConvertToNotSupported' when the ImageSource instance cannot be serialized to a string (CanSerializeToString() returns false). Only certain ImageSources (e.g. those backed by a resolvable URI) support string conversion.","triggerScenarios":"XAML serialization or designer code calling ConvertTo(instance, typeof(string)) on an ImageSource such as a dynamically rendered DrawingImage or a stream-backed BitmapImage with no UriSource.","commonSituations":"Serializing visuals created at runtime (images loaded from streams or WriteableBitmap) back to XAML; designer round-tripping controls whose ImageSource was set programmatically without a URI.","solutions":["Set the image's UriSource so it can serialize to a pack URI string","Avoid serializing non-URI-backed images; assign the image via a resource URI instead","Handle CanSerializeToString()==false by skipping serialization or storing a different representation"],"exampleFix":"// before\nvar bmp = new BitmapImage();\nbmp.BeginInit();\nbmp.StreamSource = File.OpenRead(\"img.png\"); // no UriSource\nbmp.EndInit();\nserializer.Serialize(writer, bmp); // throws\n// after\nvar bmp = new BitmapImage(new Uri(\"pack://application:,,,/img.png\"));\nserializer.Serialize(writer, bmp);","handlingStrategy":"fallback","validationCode":"if (imageSource is ImageSource src && !src.CanSerializeToString())\n    throw new NotSupportedException(\"This ImageSource cannot be serialized to a string; assign a UriSource instead.\");","typeGuard":"bool CanSerializeImage(ImageSource src) => src?.CanSerializeToString() == true;","tryCatchPattern":"try { return converter.ConvertTo(value, typeof(string)); }\ncatch (NotSupportedException) { return null; /* store bitmap via separate resource mechanism */ }","preventionTips":["Load images via UriSource (pack URIs) so they round-trip in XAML","Avoid StreamSource/WriteableBitmap for values that must be serialized","Call CanSerializeToString before serialization pipelines"],"tags":["wpf","typeconverter","not-supported","xaml-serialization"],"backgroundTag":"operation-not-supported","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"}