{"record":{"id":"f4f75a779300cad0","repo":"cefsharp/CefSharp","slug":"nameof-viewport-nameof-viewport-scale-must-b","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.OffScreen/ChromiumWebBrowser.cs","lineNumber":597,"sourceCode":"        /// are larger than the current browser <see cref=\"Size\"/>.</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 = null, int? quality = null, Viewport viewport = null)\n        {\n            ThrowExceptionIfDisposed();\n            ThrowExceptionIfBrowserNotInitialized();\n\n            using (var devToolsClient = browser.GetDevToolsClient())\n            {\n                if (viewport == null)\n                {\n                    var screenShot = await devToolsClient.Page.CaptureScreenshotAsync(format, quality, fromSurface: true).ConfigureAwait(continueOnCapturedContext: false);\n\n                    return screenShot.Data;\n                }\n\n                if (viewport.Scale <= 0)\n                {\n                    throw new ArgumentException($\"{nameof(viewport)}.{nameof(viewport.Scale)} must be greater than 0.\");\n                }\n\n                //https://github.com/chromiumembedded/cef/issues/3103\n                //CEF OSR mode doesn't set the size internally when CaptureScreenShot is called with a clip param specified, so\n                //we must manually resize our view if size is greater\n                var newWidth = viewport.Width + viewport.X;\n                if (newWidth < size.Width)\n                {\n                    newWidth = size.Width;\n                }\n                var newHeight = viewport.Height + viewport.Y;\n                if (newHeight < size.Height)\n                {\n                    newHeight = size.Height;\n                }\n\n                if ((int)newWidth > size.Width || (int)newHeight > size.Height || viewport.Scale != deviceScaleFactor)\n                {","sourceCodeStart":579,"sourceCodeEnd":615,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.OffScreen/ChromiumWebBrowser.cs#L579-L615","documentation":"Thrown by CaptureScreenshotAsync when a non-null viewport is supplied whose Scale property is <= 0. The scale is passed to CEF/DevTools which requires a positive device-scale factor; zero or negative would produce a degenerate image. The guard rejects invalid scale before resizing the view.","triggerScenarios":"Constructing a Viewport with Scale = 0 (e.g. default struct value if not explicitly set) or a negative value and passing it to CaptureScreenshotAsync.","commonSituations":"Using `new Viewport { Width = 800, Height = 600 }` and forgetting Scale (defaults to 0). Computing scale dynamically and a division produced 0.","solutions":["Set viewport.Scale to a positive value (typically 1.0 for normal DPI, or your deviceScaleFactor).","Pass viewport: null if you don't need a custom clip region.","Guard scale > 0 before calling."],"exampleFix":"// before\nvar vp = new Viewport { Width = 800, Height = 600, Scale = 0 };\nawait browser.CaptureScreenshotAsync(viewport: vp);\n\n// after\nvar vp = new Viewport { Width = 800, Height = 600, Scale = 1.0f };\nawait browser.CaptureScreenshotAsync(viewport: vp);","handlingStrategy":"validation","validationCode":"if (viewport != null && viewport.Scale <= 0) viewport.Scale = 1.0f;\nawait browser.CaptureScreenshotAsync(viewport: viewport);","typeGuard":"public static bool IsValidViewport(Viewport v) => v == null || v.Scale > 0;","tryCatchPattern":"try { return await browser.CaptureScreenshotAsync(viewport: vp); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Scale\")) { vp.Scale = 1.0f; /* retry */ }","preventionTips":["Always set viewport.Scale explicitly (typically 1.0).","Pass viewport: null when you don't need a clip region.","Guard dynamic scale calculations against producing 0 or negatives."],"tags":["offscreen","screenshot","viewport","argument"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}