{"record":{"id":"0f28c432b2769e6d","repo":"dotnet/wpf","slug":"sr-parametermustbegreaterthanzero","errorCode":null,"errorMessage":"SR.ParameterMustBeGreaterThanZero","messagePattern":"SR\\.ParameterMustBeGreaterThanZero","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/printcontext.cs","lineNumber":495,"sourceCode":"                        _writer.WriteEndElement();\n                    }\n                }\n\n                if (SpoolEdoc())\n                {\n                    _writer.Close();\n                    _writer = null;\n                    _stream.Close();\n                    _stream = null;\n                }\n            }\n        }\n\n        static private void AssertPositive(int val, string name)\n        {\n            if (val <= 0)\n            {\n                throw new ArgumentOutOfRangeException(name, SR.ParameterMustBeGreaterThanZero);\n            }\n        }\n\n        /// <summary>\n        /// Render visual to printer.\n        /// </summary>\n        private void Render(Visual visual, string uri)\n        {\n            if (visual == null)\n            {\n                throw new ArgumentNullException(\"visual\");\n            }\n\n            int dpiX  = _jobTicket.PageResolution.ResolutionX;\n            int dpiY  = _jobTicket.PageResolution.ResolutionY;\n\n            if (dpiX < 0) { dpiX = 600; }\n","sourceCodeStart":477,"sourceCodeEnd":513,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/printcontext.cs#L477-L513","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check the values passed to PrintContext/Render before calling: ensure page width and height are > 0","If dimensions come from a PrintTicket, explicitly set PageWidth/PageHeight (e.g. via new PrintTicket { PageWidth = 816, PageHeight = 1056 }) or validate the ticket","Validate printer default capabilities: read the printer's default page size and fall back to a standard size when 0/negative","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"],"exampleFix":"// before\nprintContext.Render(visual); // uses zero page size from unset PrintTicket\n// after\nif (pageWidth <= 0 || pageHeight <= 0)\n{\n    ticket.PageWidth = 816;   // 8.5in at 96dpi\n    ticket.PageHeight = 1056; // 11in at 96dpi\n}\nprintContext.Render(visual);","handlingStrategy":"validation","validationCode":"if (pageWidth <= 0 || pageHeight <= 0)\n    throw new InvalidOperationException(\"Page dimensions must be positive before printing\");","typeGuard":"bool IsValidDimension(int v) => v > 0;","tryCatchPattern":"try { printContext.Render(visual); }\ncatch (ArgumentOutOfRangeException ex) { log.Error(\"Invalid print dimension\", ex); throw new InvalidOperationException(\"Check PrintTicket page size\", ex); }","preventionTips":["Explicitly set PrintTicket PageWidth/PageHeight before rendering","Read printer capabilities and fall back to a standard page size when defaults are 0","Validate all numeric print parameters at the call site"],"tags":["wpf","printing","argument-out-of-range","printticket"],"backgroundTag":"argument-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}