babalae/better-genshin-impact · error · InvalidOperationException

Failed to create capture item.

Error message

Failed to create capture item.

What it means

CaptureHelper.CreateItemForWindow returns null, so the Windows Graphics Capture API could not create a GraphicsCaptureItem for the given window handle. This guard prevents dereferencing a null _captureItem before reading its Size and building the frame pool.

Source

Thrown at Fischless.GameCapture/Graphics/GraphicsCapture.cs:70

    public void Dispose()
    {
        Stop();
        GC.SuppressFinalize(this);
    }

    public void Start(nint hWnd, Dictionary<string, object>? settings = null)
    {
        Stop();
        try
        {
            _hWnd = hWnd;
            (_region, _captureRect) = GetGameScreenInfo(hWnd);

            _captureItem = CaptureHelper.CreateItemForWindow(_hWnd);

            if (_captureItem == null)
            {
                throw new InvalidOperationException("Failed to create capture item.");
            }

            _surfaceWidth = _captureItem.Size.Width;
            _surfaceHeight = _captureItem.Size.Height;

            // 创建D3D设备
            _d3dDevice = Direct3D11Helper.CreateDevice();

            // 创建帧池
            try
            {
                if (!_isHdrEnabled)
                {
                    // 不处理 HDR,直接抛异常走 SDR 分支
                    throw new Exception();
                }

                _pixelFormat = DirectXPixelFormat.R16G16B16A16Float;

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Verify hWnd refers to a live top-level window (IsWindow) before calling Start.
  2. Check the OS version is >= Windows 10 1903 for GraphicsCapture support.
  3. Re-acquire the game window handle and retry; on persistent failure, fall back to BitBlt or DXGI capture.
Defensive patterns

Strategy: validation

Validate before calling

if (hWnd == IntPtr.Zero || !User32.IsWindow(new HWND(hWnd)))
    return; // window not valid, do not call Start

Try / catch

try
{
    capture.Start(hWnd);
}
catch (InvalidOperationException ex) when (ex.Message == "Failed to create capture item.")
{
    // re-acquire window handle or fall back to BitBlt/DXGI
}

Prevention

When it happens

Trigger: hWnd is invalid/zero, the target window has been closed, the handle refers to a child/non-top-level window that cannot be captured, or the OS is older than Windows 10 version 1903 where GraphicsCapture is unavailable.

Common situations: Game window closed between detection and capture; running on Windows 7/8; passing an overlay or helper window handle by mistake; window minimized/destroyed at capture time.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/4f8cccc1a5182a92. Report an issue: GitHub.