{"record":{"id":"0228997d92f5d626","repo":"cefsharp/CefSharp","slug":"nameof-viewport-nameof-viewport-scale-must-b-022899","errorCode":null,"errorMessage":"{nameof(viewPort)}.{nameof(viewPort.Scale)} must be greater than 0.","messagePattern":"(.+?)\\.(.+?) must be greater than 0\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"CefSharp.WinForms/ChromiumWebBrowser.cs","lineNumber":509,"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        /// The javascript object repository, one repository per ChromiumWebBrowser instance.\n        /// </summary>\n        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\n        public IJavascriptObjectRepository JavascriptObjectRepository\n        {\n            get\n            {","sourceCodeStart":491,"sourceCodeEnd":527,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.WinForms/ChromiumWebBrowser.cs#L491-L527","documentation":"Thrown by ChromiumWebBrowser.CaptureScreenshotAsync when a Viewport argument is supplied whose Scale is zero or negative. Scale is a device-pixel multiplier applied to the capture region; a non-positive value produces an undefined/empty image, so CefSharp validates it up front before invoking the DevTools capture call.","triggerScenarios":"Passing a Viewport with Scale <= 0; defaulting a Scale field to 0 and forgetting to set it; computing Scale from a DPI or zoom ratio that evaluated to zero.","commonSituations":"DPI-aware code computing scale = 0 when DPI is unset; reusing a Viewport struct whose Scale was never initialized; dividing by an unpopulated denominator.","solutions":["Set Viewport.Scale to a positive value (1.0 for logical pixels, or the device pixel ratio) before calling CaptureScreenshotAsync.","Pass viewPort: null when you want a full-viewport capture at default scale.","Validate `viewPort == null || viewPort.Scale > 0` before invoking the method.","If computing scale dynamically, clamp it to a minimum of a small positive epsilon."],"exampleFix":"// before\nvar vp = new Viewport { X = 0, Y = 0, Width = 800, Height = 600, Scale = 0 };\nvar bytes = await browser.CaptureScreenshotAsync(viewPort: vp); // throws\n\n// after\nvar vp = new Viewport { X = 0, Y = 0, Width = 800, Height = 600, Scale = 1.0 };\nvar bytes = await browser.CaptureScreenshotAsync(viewPort: vp);","handlingStrategy":"validation","validationCode":"if (viewPort == null || viewPort.Scale > 0)\n{\n    var bytes = await browser.CaptureScreenshotAsync(viewPort: viewPort);\n}","typeGuard":"static bool IsValidViewport(Viewport vp) => vp == null || vp.Scale > 0;","tryCatchPattern":null,"preventionTips":["Default Scale to 1.0 when constructing a Viewport.","Pass null for a full-viewport capture when scale is irrelevant.","Clamp dynamically computed scale to a small positive minimum."],"tags":["cefwsharp","screenshot","validation","devtools"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}