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.PageMediaSizeName')
What it means
The PageMediaSize setter also validates that a supplied PageMediaSizeName lies within PrintSchema.PageMediaSizeNameEnumMin..Max; otherwise it throws ArgumentOutOfRangeException with parameter name 'value.PageMediaSizeName'. The name is stored as an enum-backed value, so an integer-cast name outside the schema range is rejected.
Solutions
- Check Enum.IsDefined(typeof(PageMediaSizeName), nameValue) before constructing PageMediaSize.
- Use only named PageMediaSizeName members (A4, Letter, etc.) sourced from PrintCapabilities.PageMediaSizeCollection.
- Prefer constructing PageMediaSize with explicit Width/Height when no named size matches.
- Catch ArgumentOutOfRangeException and retry with a default named size.
Example fix
// before
ticket.PageSize = new PageMediaSize((PageMediaSizeName)paperCode, w, h); // paperCode=999
// after
ticket.PageSize = Enum.IsDefined(typeof(PageMediaSizeName), paperCode)
? new PageMediaSize((PageMediaSizeName)paperCode, w, h)
: new PageMediaSize(w, h); Defensive patterns
Strategy: validation
Validate before calling
public static bool IsValidMediaSizeName(PageMediaSizeName? n) => n == null || Enum.IsDefined(typeof(PageMediaSizeName), n.Value);
Type guard
static bool IsMediaSizeName(int raw) => Enum.IsDefined(typeof(PageMediaSizeName), raw);
Try / catch
try { ticket.PageSize = new PageMediaSize(name, w, h); }
catch (ArgumentOutOfRangeException) { ticket.PageSize = new PageMediaSize(w, h); } Prevention
- Validate names with Enum.IsDefined before constructing.
- Prefer names pulled from the printer's capabilities.
- Fall back to explicit Width/Height for unknown names.
When it happens
Trigger: new PageMediaSize((PageMediaSizeName)oddInt, w, h) where oddInt is not a defined PageMediaSizeName; loading a numeric paper-size code from a legacy store and casting it directly.
Common situations: Vendor paper codes or localized paper IDs mapped onto PageMediaSizeName by numeric value; settings persisted before enum changes; user-authored config files with hand-typed integers.
Related errors
- args
- ArgumentOutOfRangeException(authenticationType)
- Specified argument was out of range of valid values…
- Specified argument was out of range of valid values…
- Specified argument was out of range of valid values…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/cf94007a6e52709c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/PrintConfig/PrtTicket_Public_Simple.cs:795
}
set
{
// first performing input parameter validation
if (value != null)
{
// either media name or the width/height values need to be specified
if ((value.PageMediaSizeName == null) &&
(value.Width == null || value.Height == null))
{
throw new ArgumentOutOfRangeException(nameof(value));
}
// if size name is specified, it needs to be valid name
if ((value.PageMediaSizeName != null) &&
(value.PageMediaSizeName < PrintSchema.PageMediaSizeNameEnumMin ||
value.PageMediaSizeName > PrintSchema.PageMediaSizeNameEnumMax))
{
throw new ArgumentOutOfRangeException("value.PageMediaSizeName");
}
// if width or height value is specified, it needs to be positive
if ((value.Width != null) && (value.Width <= 0))
{
throw new ArgumentOutOfRangeException(nameof(value),
PTUtility.GetTextFromResource("ArgumentException.PositiveValue"));
}
if ((value.Height != null) && (value.Height <= 0))
{
throw new ArgumentOutOfRangeException(nameof(value),
PTUtility.GetTextFromResource("ArgumentException.PositiveValue"));
}
}
// then adding the set operation to the cache (replacing older set operations)
if (_setterCache.ContainsKey(CapabilityName.PageMediaSize))View on GitHub (pinned to 81131a70a4)