dotnet/wpf · error · ArgumentOutOfRangeException

SR.ParameterMustBeGreaterThanZero

Error message

SR.ParameterMustBeGreaterThanZero

What it means

PrintContext.AssertPositive validates that numeric print parameters (e.g. page width/height) are strictly positive before use, throwing ArgumentOutOfRangeException with SR.ParameterMustBeGreaterThanZero. It is called from Render when constructing the print device context. A non-positive value cannot describe a valid page dimension.

Solutions

  1. Check the values passed to PrintContext/Render before calling: ensure page width and height are > 0
  2. If dimensions come from a PrintTicket, explicitly set PageWidth/PageHeight (e.g. via new PrintTicket { PageWidth = 816, PageHeight = 1056 }) or validate the ticket
  3. Validate printer default capabilities: read the printer's default page size and fall back to a standard size when 0/negative
  4. Verify DPI resolution values in the job ticket; the code substitutes 600 for negative DPI, so the offending value is more likely a page dimension

Example fix

// before
printContext.Render(visual); // uses zero page size from unset PrintTicket
// after
if (pageWidth <= 0 || pageHeight <= 0)
{
    ticket.PageWidth = 816;   // 8.5in at 96dpi
    ticket.PageHeight = 1056; // 11in at 96dpi
}
printContext.Render(visual);
Defensive patterns

Strategy: validation

Validate before calling

if (pageWidth <= 0 || pageHeight <= 0)
    throw new InvalidOperationException("Page dimensions must be positive before printing");

Type guard

bool IsValidDimension(int v) => v > 0;

Try / catch

try { printContext.Render(visual); }
catch (ArgumentOutOfRangeException ex) { log.Error("Invalid print dimension", ex); throw new InvalidOperationException("Check PrintTicket page size", ex); }

Prevention

When it happens

Trigger: Calling PrintContext/Render (via Print) where a dimension parameter passed down to AssertPositive is <= 0, typically a zero or negative page width/height derived from a PrintTicket or XPSDocumentWriter defaults.

Common situations: Printers/report drivers reporting 0-sized page dimensions; a PrintTicket with unset or zero page size; DPI fallback path leaving a dimension at 0; misconfigured custom page media size in code.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/printcontext.cs:495

                        _writer.WriteEndElement();
                    }
                }

                if (SpoolEdoc())
                {
                    _writer.Close();
                    _writer = null;
                    _stream.Close();
                    _stream = null;
                }
            }
        }

        static private void AssertPositive(int val, string name)
        {
            if (val <= 0)
            {
                throw new ArgumentOutOfRangeException(name, SR.ParameterMustBeGreaterThanZero);
            }
        }

        /// <summary>
        /// Render visual to printer.
        /// </summary>
        private void Render(Visual visual, string uri)
        {
            if (visual == null)
            {
                throw new ArgumentNullException("visual");
            }

            int dpiX  = _jobTicket.PageResolution.ResolutionX;
            int dpiY  = _jobTicket.PageResolution.ResolutionY;

            if (dpiX < 0) { dpiX = 600; }

View on GitHub (pinned to 81131a70a4)