dotnet/wpf · error · ArgumentException

SR.ReachPackaging_InvalidContentType (formatted with…

Error message

SR.ReachPackaging_InvalidContentType (formatted with contentType)

What it means

AcquireResourceStreamForXpsImage parses resourceId as a ContentType and rejects empty type/subtype. If the id is empty or has no type+subtype (AreTypeAndSubTypeEqual against ContentType.Empty), ArgumentException with SR.ReachPackaging_InvalidContentType and the offending contentType string is thrown.

Solutions

  1. Ensure resourceId is a non-empty, well-formed content type such as "image/png" or "image/jpeg".
  2. Guard the call: skip or substitute the image when the derived content type is empty.
  3. Derive the content type from the image file extension using a reliable mapping (png/jpeg/tiff/wdp) instead of raw user data.

Example fix

// before
var s = policy.AcquireResourceStreamForXpsImage(imagePath); // may be empty/invalid
// after
string contentType = Path.GetExtension(imagePath) == ".png" ? "image/png" : "image/jpeg";
if (!string.IsNullOrEmpty(contentType))
    var s = policy.AcquireResourceStreamForXpsImage(contentType);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(resourceId) || !resourceId.Contains('/')) throw new ArgumentException($"Invalid content type: '{resourceId}'", nameof(resourceId));

Type guard

static bool IsValidContentType(string id) => !string.IsNullOrWhiteSpace(id) && id.Contains('/') && id.IndexOf('/') < id.Length - 1;

Try / catch

try { var s = policy.AcquireResourceStreamForXpsImage(id); }
catch (ArgumentException ex) { log($"Bad content type '{id}' for XPS image", ex); }

Prevention

When it happens

Trigger: Passing an empty string or a string without a valid MIME-like type/subtype (e.g. "", "/images/a.png", "jpeg" without a subtype) as the resourceId to AcquireResourceStreamForXpsImage.

Common situations: Image resource ids derived from missing URI extensions; hard-coded ids without content types; data binding producing empty strings before serialization.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/XpsOMPackagingPolicy.cs:572

            }
        }

        public
        override
        XpsResourceStream
        AcquireResourceStreamForXpsImage(
            string resourceId
            )
        {
            XpsResourceStream resourceStream = null;

            ArgumentNullException.ThrowIfNull(resourceId);

            ContentType contentType = new ContentType(resourceId);

            if (ContentType.Empty.AreTypeAndSubTypeEqual(contentType))
            {
                throw new ArgumentException(SR.Format(SR.ReachPackaging_InvalidContentType, contentType.ToString()));
            }

            if (_currentXpsImageRef == 0)
            {
                try
                {
                    _currentImageType = GetXpsImageTypeFromContentType(contentType);
                    XpsPrintStream imageStreamWrapper = XpsPrintStream.CreateXpsPrintStream();
                    Uri imageUri = _xpsManager.GenerateUniqueUri(contentType);
                    _imageResourceStream = new XpsResourceStream(imageStreamWrapper, imageUri);
                    IStream imageIStream = imageStreamWrapper.GetManagedIStream();

                    IOpcPartUri partUri = GenerateIOpcPartUri(imageUri);
                    IXpsOMImageResource imageResource = _xpsOMFactory.CreateImageResource(imageIStream, _currentImageType, partUri);
                    IXpsOMImageResourceCollection imageCollection = _xpsPartResources.GetImageResources();
                    imageCollection.Append(imageResource);
                }
                catch (COMException)

View on GitHub (pinned to 81131a70a4)