{"record":{"id":"3ec912436486a638","repo":"dotnet/maui","slug":"unable-to-combine-two-hrgns","errorCode":null,"errorMessage":"Unable to combine two HRGNs.","messagePattern":"Unable to combine two HRGNs\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/WPF/Microsoft.Windows.Shell/WindowChromeWorker.cs","lineNumber":1587,"sourceCode":"\t\t\t\t(int)Math.Ceiling(radius),\n\t\t\t\t(int)Math.Ceiling(radius));\n\t\t}\n\n\t\t/// <SecurityNote>\n\t\t///   Critical : Calls critical methods\n\t\t/// </SecurityNote>\n\t\t[SecurityCritical]\n\t\t[SuppressMessage(\"Microsoft.Naming\", \"CA2204:Literals should be spelled correctly\", MessageId = \"HRGNs\")]\n\t\tprivate static void _CreateAndCombineRoundRectRgn(IntPtr hrgnSource, Rect region, double radius)\n\t\t{\n\t\t\tIntPtr hrgn = IntPtr.Zero;\n\t\t\ttry\n\t\t\t{\n\t\t\t\thrgn = _CreateRoundRectRgn(region, radius);\n\t\t\t\tCombineRgnResult result = NativeMethods.CombineRgn(hrgnSource, hrgnSource, hrgn, RGN.OR);\n\t\t\t\tif (result == CombineRgnResult.ERROR)\n\t\t\t\t{\n\t\t\t\t\tthrow new InvalidOperationException(\"Unable to combine two HRGNs.\");\n\t\t\t\t}\n\t\t\t}\n\t\t\tcatch\n\t\t\t{\n\t\t\t\tUtility.SafeDeleteObject(ref hrgn);\n\t\t\t\tthrow;\n\t\t\t}\n\t\t}\n\n\t\tprivate static bool _IsUniform(CornerRadius cornerRadius)\n\t\t{\n\t\t\tif (!DoubleUtilities.AreClose(cornerRadius.BottomLeft, cornerRadius.BottomRight))\n\t\t\t{\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tif (!DoubleUtilities.AreClose(cornerRadius.TopLeft, cornerRadius.TopRight))\n\t\t\t{","sourceCodeStart":1569,"sourceCodeEnd":1605,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/WPF/Microsoft.Windows.Shell/WindowChromeWorker.cs#L1569-L1605","documentation":"Thrown by WindowChromeWorker._CreateAndCombineRoundRectRgn when the Win32 CombineRgn API returns CombineRgnResult.ERROR while OR-ing two HRGN region handles during custom chrome rendering. This indicates the GDI region-combine operation failed at the native level. The method is used internally to build the non-rectangular (rounded-corner) window region.","triggerScenarios":"CombineRgn returns ERROR, which happens when one of the input HRGNs is invalid (zero/null handle), the GDI object table is exhausted, or the region coordinates produce an empty or degenerate geometry. Called during WindowChrome layout updates when CornerRadius > 0.","commonSituations":"Setting a large CornerRadius on WindowChrome with a very small window size; running in a low-GDI-resource environment (Terminal Services / RDP session with many open windows); or a race condition where the source HRGN was deleted by another thread before combine.","solutions":["Reduce or eliminate WindowChrome.CornerRadius to avoid HRGN round-rect creation.","Ensure the window has a non-degenerate size (Width/Height > 0) before applying rounded chrome.","Check for GDI handle leaks elsewhere in the application that may exhaust the session handle pool.","Run on a local desktop session instead of RDP if GDI resource limits are the bottleneck."],"exampleFix":"// before\n<shell:WindowChrome CornerRadius=\"20\" />\n\n// after — reduce or remove radius to avoid HRGN combine\n<shell:WindowChrome CornerRadius=\"0\" />","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    // apply WindowChrome with CornerRadius\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"HRGN\"))\n{\n    // fall back to CornerRadius = 0\n    windowChrome.CornerRadius = new CornerRadius(0);\n}","preventionTips":["Keep WindowChrome.CornerRadius modest relative to window size.","Monitor GDI handle counts in long-running apps to detect leaks.","Test custom chrome under RDP/terminal sessions where GDI resources are constrained."],"tags":["wpf","windowchrome","gdi","hrgn","win32","native-interop"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}