dotnet/maui · error · InvalidOperationException

Unable to combine two HRGNs.

Error message

Unable to combine two HRGNs.

What it means

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.

Source

Thrown at src/Compatibility/Core/src/WPF/Microsoft.Windows.Shell/WindowChromeWorker.cs:1587

				(int)Math.Ceiling(radius),
				(int)Math.Ceiling(radius));
		}

		/// <SecurityNote>
		///   Critical : Calls critical methods
		/// </SecurityNote>
		[SecurityCritical]
		[SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "HRGNs")]
		private static void _CreateAndCombineRoundRectRgn(IntPtr hrgnSource, Rect region, double radius)
		{
			IntPtr hrgn = IntPtr.Zero;
			try
			{
				hrgn = _CreateRoundRectRgn(region, radius);
				CombineRgnResult result = NativeMethods.CombineRgn(hrgnSource, hrgnSource, hrgn, RGN.OR);
				if (result == CombineRgnResult.ERROR)
				{
					throw new InvalidOperationException("Unable to combine two HRGNs.");
				}
			}
			catch
			{
				Utility.SafeDeleteObject(ref hrgn);
				throw;
			}
		}

		private static bool _IsUniform(CornerRadius cornerRadius)
		{
			if (!DoubleUtilities.AreClose(cornerRadius.BottomLeft, cornerRadius.BottomRight))
			{
				return false;
			}

			if (!DoubleUtilities.AreClose(cornerRadius.TopLeft, cornerRadius.TopRight))
			{

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Reduce or eliminate WindowChrome.CornerRadius to avoid HRGN round-rect creation.
  2. Ensure the window has a non-degenerate size (Width/Height > 0) before applying rounded chrome.
  3. Check for GDI handle leaks elsewhere in the application that may exhaust the session handle pool.
  4. Run on a local desktop session instead of RDP if GDI resource limits are the bottleneck.

Example fix

// before
<shell:WindowChrome CornerRadius="20" />

// after — reduce or remove radius to avoid HRGN combine
<shell:WindowChrome CornerRadius="0" />
Defensive patterns

Strategy: try-catch

Try / catch

try
{
    // apply WindowChrome with CornerRadius
}
catch (InvalidOperationException ex) when (ex.Message.Contains("HRGN"))
{
    // fall back to CornerRadius = 0
    windowChrome.CornerRadius = new CornerRadius(0);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/3ec912436486a638. Report an issue: GitHub.