dotnet/wpf · error · ArgumentOutOfRangeException

Specified argument was out of range of valid values…

Error message

Specified argument was out of range of valid values. (Parameter 'value')

What it means

The PagePhotoPrintingIntent setting setter validates the assigned value against PrintSchema.PhotoPrintingIntentEnumMin..Max. Values outside the PhotoPrintingIntent enum range throw ArgumentOutOfRangeException with parameter 'value'.

Solutions

  1. Only assign defined PhotoPrintingIntent enum members (None, PhotoBest, PhotoDraft, unknown/unspecified)
  2. Validate with Enum.IsDefined before casting
  3. Catch ArgumentOutOfRangeException and reset to None

Example fix

// before
intent = (PhotoPrintingIntent)comboBoxIndex;
ticket.PagePhotoPrintingIntent.Intent = intent;
// after
if (Enum.IsDefined(typeof(PhotoPrintingIntent), comboBoxIndex))
    ticket.PagePhotoPrintingIntent.Intent = (PhotoPrintingIntent)comboBoxIndex;
Defensive patterns

Strategy: validation

Validate before calling

bool IsValidPhotoPrintingIntent(int v) => v >= PrintSchema.PhotoPrintingIntentEnumMin && v <= PrintSchema.PhotoPrintingIntentEnumMax;

Type guard

static bool IsDefinedPhotoPrintingIntent(object v) => v is int i && i >= PrintSchema.PhotoPrintingIntentEnumMin && i <= PrintSchema.PhotoPrintingIntentEnumMax;

Try / catch

try { ticket.PagePhotoPrintingIntent.Intent = (PhotoPrintingIntent)value; }
catch (ArgumentOutOfRangeException) { ticket.PagePhotoPrintingIntent.Intent = PhotoPrintingIntent.None; }

Prevention

When it happens

Trigger: Assigning printTicket.PagePhotoPrintingIntentPhotoPrintingIntent/Intent with an int outside the enum, commonly a cast from unvalidated external data.

Common situations: Restoring user settings saved under an older schema version; UI controls emitting raw integer indices that exceed the enum's max.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/PrintConfig/PhotoPrintingIntent.cs:252

        /// </summary>
        /// <remarks>
        /// If the setting is not specified yet, getter will return 0.
        /// </remarks>
        /// <exception cref="ArgumentOutOfRangeException">
        /// The value to set is not one of the standard <see cref="PhotoPrintingIntent"/>.
        /// </exception>
        public PhotoPrintingIntent Value
        {
            get
            {
                return (PhotoPrintingIntent)this[PrintSchemaTags.Framework.OptionNameProperty];
            }
            set
            {
                if (value < PrintSchema.PhotoPrintingIntentEnumMin ||
                    value > PrintSchema.PhotoPrintingIntentEnumMax)
                {
                    throw new ArgumentOutOfRangeException(nameof(value));
                }

                this[PrintSchemaTags.Framework.OptionNameProperty] = (int)value;
            }
        }

        #endregion Public Properties

        #region Public Methods

        /// <summary>
        /// Converts the page photo printing intent setting to human-readable string.
        /// </summary>
        /// <returns>A string that represents this page photo printing intent setting.</returns>
        public override string ToString()
        {
            return Value.ToString();
        }

View on GitHub (pinned to 81131a70a4)