dotnet/wpf · error · ArgumentException

' ' ContentType is not valid.

Error message

'{0}' ContentType is not valid.

What it means

AddImage(ContentType) throws XpsPackagingException(SR.ReachPackaging_UnsupportedImageType) when XpsManager.SupportedImageType(mimeType) returns false, i.e. the ContentType is not one of the image formats XPS supports. XPS only accepts specific image MIME types.

Solutions

  1. Convert the image to PNG, JPEG, TIFF, or WDP before adding it.
  2. Check XpsManager.SupportedImageType(mimeType) (or check mime against the allowed list) before calling AddImage.
  3. Map unsupported MIME types to a supported equivalent (e.g. BMP → PNG).

Example fix

// before
page.AddImage(new ContentType("image/bmp")); // throws
// after
if (!XpsManager.SupportedImageType(ct))
    ct = new ContentType("image/png"); // convert image first
page.AddImage(ct);
Defensive patterns

Strategy: validation

Validate before calling

string[] allowed = { "image/png", "image/jpeg", "image/tiff", "image/vnd.ms-photo" };
if (!allowed.Contains(mimeType)) throw new NotSupportedException($"{mimeType} is not an XPS-supported image type");

Try / catch

try { page.AddImage(ct); }
catch (XpsPackagingException) { image = ConvertToSupported(image); page.AddImage(new ContentType(image.Mime)); }

Prevention

When it happens

Trigger: Calling AddImage with a ContentType whose type/subtype is not JPG, TIF, WDP (JPEG XR / vnd.ms-photo), or PNG — e.g. "image/gif" or "image/bmp".

Common situations: Converting documents that embed BMP/GIF images into XPS; deriving the MIME type from a file extension and passing an unsupported one.

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/0cfad4b384dd57f7. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedPageReaderWriter.cs:1245

            return AddImage(new ContentType(mimeType));
        }


        internal
        XpsImage
        AddImage(
            ContentType mimeType
            )
        {
            if (null == _metroPart)
            {
                throw new ObjectDisposedException("FixedPageReader");
            }
            ArgumentNullException.ThrowIfNull(mimeType);
            if (ContentType.Empty.AreTypeAndSubTypeEqual(mimeType))
            {
                throw new ArgumentException(SR.Format(SR.ReachPackaging_InvalidContentType, mimeType.ToString()));
            }

            if (!XpsManager.SupportedImageType(mimeType))
            {
                throw new XpsPackagingException(SR.ReachPackaging_UnsupportedImageType);
            }

            //
            // Create the part and writer
            //
            PackagePart metroPart = this.CurrentXpsManager.GenerateUniquePart(mimeType);
            return AddImage(metroPart);
}


        /// <summary>
        /// This method retrieves a image resource part from a Xps
        /// package using the given URI.

View on GitHub (pinned to 81131a70a4)