dotnet/wpf · error · ArgumentOutOfRangeException
PTUtility.GetTextFromResource("ArgumentException.PositiveVal…
Error message
PTUtility.GetTextFromResource("ArgumentException.PositiveValue") What it means
SetFixedMediaSize (via InternalSetFixedMediaSize) accepts an explicit media size width/height; when width or height is <= 0 it throws ArgumentOutOfRangeException naming the offending parameter. Fixed media dimensions must be positive physical values.
Solutions
- Verify width and height are > 0 before calling SetFixedMediaSize.
- Use a PageMediaSize from queue.GetPrintCapabilities().PageMediaSizes instead of computing dimensions manually.
- Guard with Math.Max or explicit checks and log/skip when dimensions are non-positive.
- Catch ArgumentOutOfRangeException and fall back to the queue's default media size.
Example fix
// before
ticket.SetFixedMediaSize(width, 0);
// after
if (width > 0 && height > 0)
ticket.SetFixedMediaSize(width, height);
else
ticket.PageMediaSize = queue.DefaultPrintTicket.PageMediaSize; Defensive patterns
Strategy: validation
Validate before calling
if (width > 0 && height > 0)
ticket.SetFixedMediaSize(width, height); Type guard
bool IsValidMediaSize(double w, double h) => w > 0 && h > 0;
Try / catch
try { ticket.SetFixedMediaSize(width, height); }
catch (ArgumentOutOfRangeException) { ticket.PageMediaSize = queue.DefaultPrintTicket.PageMediaSize; } Prevention
- Prefer PageMediaSize objects from queue.GetPrintCapabilities().PageMediaSizes
- Check unit conversions for zero results before applying sizes
- Treat unset/zero dimensions as 'use default' rather than passing them through
When it happens
Trigger: Calling PrintTicket.SetFixedMediaSize(width, height) with mediaSizeWidth <= 0 or mediaSizeHeight <= 0.
Common situations: Computing page size from uninitialized/zero variables; unit conversion mistakes producing 0; passing PageMediaSize.Width/Height of a default-constructed object.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- ArgumentException.NonNegativeValue (Parameter 'squareScale')
- ArgumentException.PositiveValue (Parameter 'value')
- ArgumentException.PositiveValue (Parameter 'value')
- PTUtility.GetTextFromResource("ArgumentException.PositiveVal…
- Specified argument was out of range of valid values…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/c14c254f9d1b4dbb.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/PrintConfig/PageMediaSize.cs:562
bool bSetValue,
double mediaSizeWidth,
double mediaSizeHeight,
bool bSetWH)
{
if (bSetValue)
{
if (value < PrintSchema.PageMediaSizeNameEnumMin ||
value > PrintSchema.PageMediaSizeNameEnumMax)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
}
if (bSetWH)
{
if ( (mediaSizeWidth <= 0) || (mediaSizeHeight <= 0) )
{
throw new ArgumentOutOfRangeException((mediaSizeWidth <= 0) ? "mediaSizeWidth" : "mediaSizeHeight",
PTUtility.GetTextFromResource("ArgumentException.PositiveValue"));
}
}
// Remove all XML content related to PageMediaSize feature
ClearSetting();
if (bSetValue)
{
this[PrintSchemaTags.Framework.OptionNameProperty] = (int)value;
}
if (bSetWH)
{
this[PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeWidth] =
UnitConverter.LengthValueFromDIPToMicron(mediaSizeWidth);
this[PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeHeight] =
UnitConverter.LengthValueFromDIPToMicron(mediaSizeHeight);View on GitHub (pinned to 81131a70a4)