dotnet/wpf · error · ArgumentException

String passed for type is not valid.

Error message

String passed for type is not valid.

What it means

FixedPageReader.AddImage(string) throws ArgumentException(SR.ReachPackaging_InvalidType) when the mimeType string is empty (length 0). A null mimeType would already fail via ArgumentNullException.ThrowIfNull; an empty string is rejected because a MIME type must have a type/subtype.

Solutions

  1. Pass a valid image MIME type such as "image/png", "image/jpeg", "image/tiff", or "image/vnd.ms-photo".
  2. Validate the MIME string is non-empty before calling AddImage.

Example fix

// before
string mime = config["ImageMime"]; // ""
page.AddImage(mime);
// after
string mime = config["ImageMime"] ?? "image/png";
if (string.IsNullOrEmpty(mime)) throw new InvalidOperationException("Image MIME type required");
page.AddImage(mime);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(mimeType)) throw new ArgumentException("MIME type must be non-empty", nameof(mimeType));

Try / catch

try { page.AddImage(mime); }
catch (ArgumentException ex) when (ex.Message.Contains("type")) { /* substitute default: */ page.AddImage("image/png"); }

Prevention

When it happens

Trigger: Calling AddImage("") on an XpsFixedPageReaderWriter whose part is still live.

Common situations: MIME type read from an empty config field, a failed lookup, or a string variable default-initialized to empty string.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

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

        /// </returns>
        /// <exception cref="ObjectDisposedException">FixedPageReader has already been disposed</exception>
        /// <exception cref="ArgumentNullException">imageType is null.</exception>
        /// <exception cref="ArgumentException">imageType is an empty string.</exception>
        /// <exception cref="SR.ReachPackaging_UnsupportedImageType">Unsupported image type</exception>
        public
        XpsImage
        AddImage(
            string  mimeType
            )
        {
            if (null == _metroPart)
            {
                throw new ObjectDisposedException("FixedPageReader");
            }
            ArgumentNullException.ThrowIfNull(mimeType);
            if (0 == mimeType.Length)
            {
                throw new ArgumentException(SR.ReachPackaging_InvalidType);
            }

            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))

View on GitHub (pinned to 81131a70a4)