{"record":{"id":"8d635b583e4eb5d9","repo":"java-native-access/jna","slug":"window-width-and-or-height-were-0-even-though-getwindowrect","errorCode":null,"errorMessage":"Window width and/or height were 0 even though GetWindowRect did not appear to fail.","messagePattern":"Window width and/or height were 0 even though GetWindowRect did not appear to fail\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/GDI32Util.java","lineNumber":85,"sourceCode":"     * @return the window captured as a screenshot, or null if the BufferedImage\n     *         doesn't construct properly\n     *\n     * @throws IllegalStateException if the rectangle from GetWindowRect has a\n     *                               width and/or height of 0. <br>\n     * if the device context acquired from the original HWND doesn't release\n     * properly\n     */\n    public static BufferedImage getScreenshot(HWND target) {\n        RECT rect = new RECT();\n        if (!User32.INSTANCE.GetWindowRect(target, rect)) {\n            throw new Win32Exception(Native.getLastError());\n        }\n        Rectangle jRectangle = rect.toRectangle();\n        int windowWidth = jRectangle.width;\n        int windowHeight = jRectangle.height;\n\n        if (windowWidth == 0 || windowHeight == 0) {\n            throw new IllegalStateException(\"Window width and/or height were 0 even though GetWindowRect did not appear to fail.\");\n        }\n\n        HDC hdcTarget = User32.INSTANCE.GetDC(target);\n        if (hdcTarget == null) {\n            throw new Win32Exception(Native.getLastError());\n        }\n\n        Win32Exception we = null;\n\n        // device context used for drawing\n        HDC hdcTargetMem = null;\n\n        // handle to the bitmap to be drawn to\n        HBITMAP hBitmap = null;\n\n        // original display surface associated with the device context\n        HANDLE hOriginal = null;\n","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/GDI32Util.java#L67-L103","documentation":"GDI32Util.getScreenshot throws this IllegalStateException when GetWindowRect reported success but the resulting rectangle has zero width or height, making a bitmap capture impossible. It is a sanity check guarding Blt/copy operations that would fail or produce an empty image.","triggerScenarios":"Capturing a window that is minimized, not yet shown (zero-size client area), or whose rect conversion yields 0 dimensions; passing an HWND of a hidden window.","commonSituations":"Screenshotting a minimized window via automation; capturing during window creation before layout; targeting a message-only or zero-size window.","solutions":["Ensure the target window is visible/restored before capture (ShowWindow with SW_RESTORE, or wait for it to be shown)","Check IsWindowVisible and IsIconic and restore/bring the window forward first","Verify the HWND is the correct top-level window, not a zero-size child or dummy handle","If the zero size is legitimate for your case, skip the screenshot instead of calling getScreenshot"],"exampleFix":"// before\nBufferedImage img = GDI32Util.getScreenshot(hwnd);\n// after\nif (!User32.INSTANCE.IsWindowVisible(hwnd) || User32.INSTANCE.IsIconic(hwnd)) {\n    User32.INSTANCE.ShowWindow(hwnd, new WinDef.SW_NOSHOW(SW_RESTORE));\n}\nRECT rect = new RECT();\nUser32.INSTANCE.GetWindowRect(hwnd, rect);\nif (rect.toRectangle().width > 0 && rect.toRectangle().height > 0) {\n    BufferedImage img = GDI32Util.getScreenshot(hwnd);\n}","handlingStrategy":"validation","validationCode":"RECT r = new RECT();\nif (!User32.INSTANCE.GetWindowRect(hwnd, r)) throw new Win32Exception(Kernel32.INSTANCE.GetLastError());\nif (r.toRectangle().width == 0 || r.toRectangle().height == 0) {\n    User32.INSTANCE.ShowWindow(hwnd, 9 /* SW_RESTORE */);\n}","typeGuard":"boolean isScreenshottable(WinDef.HWND hwnd) {\n    RECT r = new RECT();\n    return User32.INSTANCE.IsWindowVisible(hwnd)\n        && !User32.INSTANCE.IsIconic(hwnd)\n        && User32.INSTANCE.GetWindowRect(hwnd, r)\n        && r.toRectangle().width > 0 && r.toRectangle().height > 0;\n}","tryCatchPattern":"try {\n    BufferedImage img = GDI32Util.getScreenshot(hwnd);\n} catch (IllegalStateException e) {\n    // window zero-size: restore and retry once\n    User32.INSTANCE.ShowWindow(hwnd, 9);\n    BufferedImage img = GDI32Util.getScreenshot(hwnd);\n}","preventionTips":["Restore/un-minimize the window and wait until it is visible before capturing","Check IsIconic/IsWindowVisible and GetWindowRect dimensions before calling getScreenshot","Handle windows that legitimately have zero size by skipping capture"],"tags":["java","win32","gdi","screenshot","window"],"backgroundTag":"invalid-state-transition","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}