{"record":{"id":"ab10f5031e0de14b","repo":"cefsharp/CefSharp","slug":"viewport-scale-must-be-greater-than-0","errorCode":null,"errorMessage":"viewPort.Scale must be greater than 0.","messagePattern":"viewPort\\.Scale must be greater than 0\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"CefSharp.Wpf/HwndHost/ChromiumWebBrowser.cs","lineNumber":1920,"sourceCode":"        }\n\n        /// <summary>\n        /// Capture page screenshot.\n        /// </summary>\n        /// <param name=\"format\">Image compression format (defaults to png).</param>\n        /// <param name=\"quality\">Compression quality from range [0..100] (jpeg only).</param>\n        /// <param name=\"viewPort\">Capture the screenshot of a given region only.</param>\n        /// <param name=\"fromSurface\">Capture the screenshot from the surface, rather than the view. Defaults to true.</param>\n        /// <param name=\"captureBeyondViewport\">Capture the screenshot beyond the viewport. Defaults to false.</param>\n        /// <returns>A task that can be awaited to obtain the screenshot as a byte[].</returns>\n        public async Task<byte[]> CaptureScreenshotAsync(CaptureScreenshotFormat format = CaptureScreenshotFormat.Png, int? quality = null, Viewport viewPort = null, bool fromSurface = true, bool captureBeyondViewport = false)\n        {\n            ThrowExceptionIfDisposed();\n            ThrowExceptionIfBrowserNotInitialized();\n\n            if (viewPort != null && viewPort.Scale <= 0)\n            {\n                throw new ArgumentException($\"{nameof(viewPort)}.{nameof(viewPort.Scale)} must be greater than 0.\");\n            }\n\n            using (var devToolsClient = browser.GetDevToolsClient())\n            {\n                var screenShot = await devToolsClient.Page.CaptureScreenshotAsync(format, quality, viewPort, fromSurface, captureBeyondViewport).ConfigureAwait(continueOnCapturedContext: false);\n\n                return screenShot.Data;\n            }\n        }\n\n        /// <summary>\n        /// Throw exception if browser not initialized.\n        /// </summary>\n        /// <exception cref=\"Exception\">Thrown when an exception error condition occurs.</exception>\n        private void ThrowExceptionIfBrowserNotInitialized()\n        {\n            if (!InternalIsBrowserInitialized())\n            {","sourceCodeStart":1902,"sourceCodeEnd":1938,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.Wpf/HwndHost/ChromiumWebBrowser.cs#L1902-L1938","documentation":"CaptureScreenshotAsync validates the optional Viewport parameter: its Scale must be strictly greater than zero because CEF uses it to compute the capture rectangle. A zero or negative scale produces an invalid capture region, so the call is rejected before reaching the DevTools client.","triggerScenarios":"Passing a Viewport whose Scale property is 0 or negative. The default-constructed Viewport may have Scale = 0 depending on how it was created.","commonSituations":"Computing Scale from a DPI ratio or zoom factor that evaluates to 0 (e.g. dividing by a zero screen dimension). Reusing a Viewport object that was partially initialized.","solutions":["Set viewPort.Scale to a positive value (typically 1.0 for 1:1 capture) before calling CaptureScreenshotAsync.","Pass null for viewPort to capture the full viewport at default scale.","Validate viewPort?.Scale > 0 before the call."],"exampleFix":"// before\nvar vp = new Viewport { X = 0, Y = 0, Width = 800, Height = 600, Scale = 0 };\nvar bytes = await browser.CaptureScreenshotAsync(CaptureScreenshotFormat.Png, null, vp);\n\n// after\nvar vp = new Viewport { X = 0, Y = 0, Width = 800, Height = 600, Scale = 1.0 };\nvar bytes = await browser.CaptureScreenshotAsync(CaptureScreenshotFormat.Png, null, vp);","handlingStrategy":"validation","validationCode":"if (viewPort != null && viewPort.Scale <= 0)\n{\n    throw new ArgumentException(\n        $\"viewPort.Scale must be > 0, got {viewPort.Scale}\",\n        nameof(viewPort));\n}\n// or simply default it\nif (viewPort != null && viewPort.Scale <= 0)\n    viewPort.Scale = 1.0;\n\nawait browser.CaptureScreenshotAsync(format, quality, viewPort);","typeGuard":null,"tryCatchPattern":"try\n{\n    await browser.CaptureScreenshotAsync(format, quality, viewPort);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"viewPort.Scale\"))\n{\n    // retry with default scale\n    viewPort.Scale = 1.0;\n    await browser.CaptureScreenshotAsync(format, quality, viewPort);\n}","preventionTips":["Always set Viewport.Scale to 1.0 unless a specific DPI-scaled capture is needed.","Pass null for viewPort when capturing the full viewport at default scale.","Validate computed scale values before passing."],"tags":["screenshot","viewport","validation","wpf-hwndhost"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}