dotnet/wpf · error · NotSupportedException
Converter_ConvertToNotSupported
Error message
Converter_ConvertToNotSupported
What it means
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.
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
Example fix
// before
var bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = File.OpenRead("img.png"); // no UriSource
bmp.EndInit();
serializer.Serialize(writer, bmp); // throws
// after
var bmp = new BitmapImage(new Uri("pack://application:,,,/img.png"));
serializer.Serialize(writer, bmp); Defensive patterns
Strategy: fallback
Validate before calling
if (imageSource is ImageSource src && !src.CanSerializeToString())
throw new NotSupportedException("This ImageSource cannot be serialized to a string; assign a UriSource instead."); Type guard
bool CanSerializeImage(ImageSource src) => src?.CanSerializeToString() == true;
Try / catch
try { return converter.ConvertTo(value, typeof(string)); }
catch (NotSupportedException) { return null; /* store bitmap via separate resource mechanism */ } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- SR.Converter_ConvertToNotSupported
- SR.Converter_ConvertToNotSupported
- By default, ToolTip property does not support ToolTip…
- Cannot convert from type.
- Cannot convert from type.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/69eb9780a2f1c9bc.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ImageSourceConverter.cs:197
/// </exception>
/// <param name="context"> The ITypeDescriptorContext for this call. </param>
/// <param name="culture"> The CultureInfo which is respected when converting. </param>
/// <param name="value"> The object to convert to an instance of "destinationType". </param>
/// <param name="destinationType"> The type to which this will convert the ImageSource instance. </param>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType != null && value is ImageSource)
{
ImageSource instance = (ImageSource)value;
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 (!instance.CanSerializeToString())
{
throw new NotSupportedException(SR.Converter_ConvertToNotSupported);
}
}
// Delegate to the formatting/culture-aware ConvertToString method.
return instance.ConvertToString(null, culture);
}
}
// Pass unhandled cases to base class (which will throw exceptions for null value or destinationType.)
return base.ConvertTo(context, culture, value, destinationType);
}
/// Try to get a bitmap out of a byte array. This is an ole format that Access uses.
/// this fails very quickly so we can try this first without a big perf hit.
private unsafe Stream GetBitmapStream(byte[] rawData)
{
Debug.Assert(rawData != null, "rawData is null.");
fixed (byte* pByte = rawData)View on GitHub (pinned to 81131a70a4)