dotnet/wpf · error · ArgumentOutOfRangeException

Value must be positive.

Error message

Value must be positive.

What it means

The PrintTicket.PageResolution setter requires that a specified X resolution (DPI) be strictly positive. Assigning a PageResolution whose X value is 0 or negative throws ArgumentOutOfRangeException with the message 'Value must be positive.', because zero or negative DPI cannot represent a printer resolution.

Solutions

  1. Ensure the X resolution value is a positive DPI number (>= 1) before constructing the PageResolution.
  2. Guard the source of the DPI value (config/UI) for > 0 before assignment.
  3. Use null for X (and supply QualitativeResolution) when the dimension is not known.

Example fix

// before
var dpiX = CalculateDpi(); // may be 0
ticket.PageResolution = new PageResolution(dpiX, 600);
// after
if (dpiX > 0) ticket.PageResolution = new PageResolution(dpiX, 600);
Defensive patterns

Strategy: validation

Validate before calling

if (dpiX > 0)
    ticket.PageResolution = new PageResolution(dpiX, dpiY);
else
    throw new InvalidOperationException("X DPI must be positive");

Type guard

bool IsValidResolutionX(PageResolution r) => r == null || r.X == null || r.X > 0;

Try / catch

try { ticket.PageResolution = new PageResolution(dpiX, dpiY); }
catch (ArgumentOutOfRangeException ex) when (ex.Message.Contains("positive"))
{ Log.Error($"Invalid X DPI {dpiX}"); }

Prevention

When it happens

Trigger: Assigning ticket.PageResolution = new PageResolution(0, 600) or new PageResolution(-300, 600, quality), i.e. any PageResolution with X != null and X <= 0.

Common situations: Computing DPI from user input or division that yields 0; using default(int) = 0 as placeholder; negative values from scaling calculations on small page sizes.

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


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/PrintConfig/PrtTicket_Public_Simple.cs:1043

                {
                    if ((value.QualitativeResolution == null) &&
                        (value.X == null || value.Y == null))
                    {
                        throw new ArgumentOutOfRangeException(nameof(value));
                    }

                    // If quality lable is specified, it needs to be valid value
                    if ((value.QualitativeResolution != null) &&
                        (value.QualitativeResolution < PrintSchema.PageQualitativeResolutionEnumMin ||
                         value.QualitativeResolution > PrintSchema.PageQualitativeResolutionEnumMax))
                    {
                        throw new ArgumentOutOfRangeException(nameof(value));
                    }

                    // If specified, resolution X and Y values must be positive
                    if ((value.X != null) && (value.X <= 0))
                    {
                        throw new ArgumentOutOfRangeException(nameof(value),
                                      PTUtility.GetTextFromResource("ArgumentException.PositiveValue"));
                    }

                    if ((value.Y != null) && (value.Y <= 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.PageResolution))
                {
                    _setterCache.Remove(CapabilityName.PageResolution);
                }

                _setterCache.Add(CapabilityName.PageResolution, value);

View on GitHub (pinned to 81131a70a4)