dotnet/wpf · error · XpsSerializationException

No image service is registered with resource policy.

Error message

No image service is registered with resource policy.

What it means

ImageSourceTypeConverter.ConvertTo obtains an image encoding service from the serialization manager's ResourcePolicy. When GetService(typeof(XpsImageSerializationService)) returns null, the converter cannot acquire an encoder or write the image part into the XPS package and throws XpsSerializationException with ReachSerialization_NoImageService.

Solutions

  1. Run image serialization through the standard XpsDocumentWriter/XpsSerializationManager pipeline, which registers XpsImageSerializationService.
  2. If using a custom manager, register an XpsImageSerializationService on its ResourcePolicy before ConvertTo.
  3. Catch XpsSerializationException and fall back to writing the image part manually with an appropriate BitmapEncoder.

Example fix

// before
var manager = new PackageSerializationManager(); // no image service
converter.ConvertTo(context, culture, bitmapSource, typeof(Uri));

// after
using (XpsDocument doc = new XpsDocument(pkgPath, FileAccess.ReadWrite))
{
    var writer = XpsDocument.CreateXpsDocumentWriter(doc);
    writer.Write(visual); // full pipeline registers the image service
}
Defensive patterns

Strategy: try-catch

Validate before calling

var manager = (PackageSerializationManager)context.GetService(typeof(XpsSerializationManager));
bool hasImageService = manager?.ResourcePolicy?.GetService(typeof(XpsImageSerializationService)) != null;
if (!hasImageService) throw new InvalidOperationException("Image serialization requires XpsImageSerializationService.");

Type guard

static bool HasImageService(IServiceProvider provider) =>
    provider?.GetService(typeof(XpsImageSerializationService)) is XpsImageSerializationService;

Try / catch

try
{
    converter.ConvertTo(context, culture, bitmapSource, typeof(Uri));
}
catch (XpsSerializationException ex) when (ex.Message.Contains("image service"))
{
    // write the image part manually with PngBitmapEncoder/JpegBitmapEncoder
}

Prevention

When it happens

Trigger: Calling ConvertTo with a context whose XpsSerializationManager/PackageSerializationManager has a ResourcePolicy lacking an XpsImageSerializationService registration — e.g. a hand-built serialization manager or a pipeline that only registered font services.

Common situations: Custom XPS packaging hosts missing image resource services; reusing a serialization manager configured for a different resource kind; invoking the converter outside the XpsDocumentWriter save flow.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


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

Appendix: source

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

                   // Also, add a relationship for the current page to this image
                   // resource.  This is needed to conform with Xps specification.
                   //
                   manager.AddRelationshipToCurrentPage(imageUri, XpsS0Markup.ResourceRelationshipName);
                   currentPageImageTable.Add(uriHashCode, imageUri);
                }
            }
            else
            {
                //
                // This image as never serialized before so we will do it now.
                // Retrieve the image serialization service from the resource policy
                //
                IServiceProvider resourceServiceProvider = manager.ResourcePolicy;

                XpsImageSerializationService imageService = (XpsImageSerializationService)resourceServiceProvider.GetService(typeof(XpsImageSerializationService));
                if (imageService == null)
                {
                    throw new XpsSerializationException(SR.ReachSerialization_NoImageService);
                }

                //
                // Obtain a valid encoder for the image.
                //
                BitmapEncoder encoder = imageService.GetEncoder(bitmapSource);
                string imageMimeType = GetImageMimeType(encoder);

                bool isSupportedMimeType = imageService.IsSupportedMimeType(bitmapSource);

                //
                // Acquire a writable stream from the serialization manager and encode
                // and serialize the image into the stream.
                //
                XpsResourceStream resourceStream = manager.AcquireResourceStream(typeof(BitmapSource), imageMimeType);
                bool bCopiedStream = false;

                BitmapFrame bitmapFrame = bitmapSource as BitmapFrame;

View on GitHub (pinned to 81131a70a4)