> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XGameStreamingSetResolution

> XGameStreamingSetResolution

# XGameStreamingSetResolution

设置流的分辨率。

## 语法

```cpp theme={null}
HRESULT XGameStreamingSetResolution(
        uint32_t width,
        uint32_t height
)
```

### 参数

*width*   \_In\_\
类型：uint32\_t

要将流分辨率设置为的宽度。

*height*   \_In\_\
类型：uint32\_t

要将流分辨率设置为的高度。

### 返回值

类型：HRESULT

如果成功，则返回 **S\_OK**；否则返回错误代码。

#### 潜在错误

| 错误代码                               | 错误值        | 错误原因                                                                |
| ---------------------------------- | ---------- | ------------------------------------------------------------------- |
| E\_GAMESTREAMING\_NOT\_INITIALIZED | 0x89245400 | XGameStreaming 运行时尚未初始化。在调用其他 API 之前，请先调用 XGameStreamingInitialize。 |
| E\_INVALIDARG                      | 0x80070057 | *width* 和/或 *height* 参数或硬件不满足要求。                                    |

有关错误代码列表，请参阅[错误代码](/reference/errorcodes)。

## 备注

此 API 使用非标准分辨率时，仅在 XBOX Game Streaming 服务器、XBOX Series X Dev Kit 和 XBOX Series S Test Kit 上会成功。标准分辨率（如 720p 和 1080p）无论硬件如何都会成功。原因是零售版 XBOX 只能通过 Remote Play 进行流式处理，并且可能仍在向电视输出视频，因此使用非标准分辨率会产生失真的图像。

宽度和高度必须至少为 640x360，最大值可以在 [XGameStreamingDisplayDetails](/reference/system/xgamestreaming/structs/xgamestreamingdisplaydetails) 的 `maxWidth` 和 `maxHeight` 字段中找到，可以通过 [XGameStreamingGetDisplayDetails](/reference/system/xgamestreaming/functions/xgamestreaminggetdisplaydetails) 检索。

由 `width` \* `height` 计算得出的最大像素数必须小于或等于编码器的最大值。可以使用 [XGameStreamingGetDisplayDetails](/reference/system/xgamestreaming/functions/xgamestreaminggetdisplaydetails) API 找到最大值。此值将来可能会增大。

`width` 和 `height` 必须能被 8 整除。

可以根据需要频繁调用此 API，但是流分辨率每 200 毫秒只能更改一次。在该 200 毫秒时间窗口内发生的任何调用中的最后一次分辨率将在该窗口过后应用。

请注意，此 API 将为所有已连接的客户端设置流的分辨率。这意味着必须特别考虑当连接了多个客户端时，游戏的最佳选项是什么。这可能意味着使用标准的 16:9 分辨率，也可能意味着尝试找到最适合所有客户端的分辨率。

<Note>更改流分辨率会修改 ID3D12CommandQueue::PresentX 中的视图矩形缩放。D3D12XBOX\_PRESENT\_PLANE\_PARAMETERS.pDestPlacementBase 的 0.0-1.0 缩放因子引用传递给 XGameStreamingSetResolution 的高度和宽度，而不是它们通常引用的标准 1920x1080 或 3840x2160 分辨率。</Note>

## 示例

```C++ theme={null}

#define DEFAULT_GAME_WIDTH 1920
#define DEFAULT_GAME_HEIGHT 1080

#define GAME_WIDEST_SUPPORTED_ASPECT_RATIO 21.5f / 9.0f
#define GAME_TALLEST_SUPPORTED_ASPECT_RATIO 16.0f / 10.0f

static uint32_t s_currentStreamWidth = DEFAULT_GAME_WIDTH;
static uint32_t s_currentStreamHeight = DEFAULT_GAME_HEIGHT;

// Option 1: Event driven. Note: be aware of potential threading issues when using the task queue.
void GameStreamingClientManager::OnConnectionStateChanged(XGameStreamingClientId client, XGameStreamingConnectionState connected)
{
    // Other connection work like registering or unregistering for the client properties change events.
    ...

    UpdateResolutionIfNeeded();
}

void GameStreamingClientManager::OnClientPropertiesChanged(
    XGameStreamingClientId client,
    uint32_t updatedPropertiesCount,
    XGameStreamingClientProperty* updatedProperties)
{
    for (uint32_t i = 0; i < updatedPropertiesCount; ++i)
    {
        switch (updatedProperties[i])
        {
        case XGameStreamingClientProperty::DisplayDetails:
        {
            UpdateResolutionIfNeeded();
            break;
        }

        default:
            // A characteristic we are not tracking - do nothing
            break;
        }
    }
}

// Option 2: Polling.

void Game::Update(DX::StepTimer const& timer)
{
    ...

    gameStreamingClientManager->UpdateResolutionIfNeeded();

    ...
}

void GameStreamingClientManager::UpdateResolutionIfNeeded()
{
    bool changeResolution = false;
    bool useDefaultResolution = true;

    // Only use custom resolution when there is only one streaming client connected.
    if (XGameStreamingGetClientCount() == 1)
    {
        XGameStreamingClientId client;
        uint32_t clientsUsed = 0;
        HRESULT hr = XGameStreamingGetClients(1, &client, &clientsUsed);
        if (SUCCEEDED(hr) && clientsUsed == 1)
        {
            XGameStreamingDisplayDetails displayDetails = {};
            hr = XGameStreamingGetDisplayDetails(client, DEFAULT_GAME_WIDTH * DEFAULT_GAME_HEIGHT, GAME_WIDEST_SUPPORTED_ASPECT_RATIO, GAME_TALLEST_SUPPORTED_ASPECT_RATIO, &displayDetails);

            if (SUCCEEDED(hr))
            {
                useDefaultResolution = false;

                // Assuming the game supports all resolutions, use the stream resolution to the preferred dimensions as provided.
                if (s_currentStreamWidth != displayDetails.preferredWidth || s_currentStreamHeight != displayDetails.preferredHeight)
                {
                    changeResolution = true;
                    s_currentStreamWidth = displayDetails.preferredWidth;
                    s_currentStreamHeight = displayDetails.preferredHeight;
                }
            }
            else
            {
                LogFormat(L"XGameStreamingGetDisplayDetails failed %x", hr);
            }
        }
        else
        {
            LogFormat(L"XGameStreamingGetClients failed hr=%x clientsUsed=%d", hr, clientsUsed);
        }
    }

    if (useDefaultResolution)
    {
        if (s_currentStreamWidth != DEFAULT_GAME_WIDTH || s_currentStreamHeight != DEFAULT_GAME_HEIGHT)
        {
            changeResolution = true;
            s_currentStreamWidth = DEFAULT_GAME_WIDTH;
            s_currentStreamHeight = DEFAULT_GAME_HEIGHT;
        }
    }

    if (changeResolution)
    {
        // Update the stream to the new resolution.
        HRESULT hr = XGameStreamingSetResolution(s_currentStreamWidth, s_currentStreamHeight);
        if (SUCCEEDED(hr))
        {
            // Update the game to render at the new resolution.
        }
        else
        {
            LogFormat(L"XGameStreamingSetResolution failed %x", hr);
        }
    }
}

```

## 要求

**标头：** xgamestreaming.h\
**库：** xgameruntime.lib\
**支持的平台：** Windows、XBOX One 系列主机和 XBOX Series 主机

## 另请参阅

[XGameStreaming](/reference/system/xgamestreaming/xgamestreaming_members)\
[XGameStreamingDisplayDetails](/reference/system/xgamestreaming/structs/xgamestreamingdisplaydetails)\
[XGameStreamingGetDisplayDetails](/reference/system/xgamestreaming/functions/xgamestreaminggetdisplaydetails)\
[自定义分辨率概述](/build/core-features/common/game-streaming/game-streaming-custom-resolution-overview)


## Related topics

- [XGameStreaming](/zh-CN/reference/system/xgamestreaming/xgamestreaming_members.md)
- [XGameStreamingGetDisplayDetails](/zh-CN/reference/system/xgamestreaming/functions/xgamestreaminggetdisplaydetails.md)
- [游戏串流自定义分辨率最佳实践](/zh-CN/build/core-features/common/game-streaming/game-streaming-custom-resolution-best-practices.md)
- [自定义分辨率概述](/zh-CN/build/core-features/common/game-streaming/game-streaming-custom-resolution-overview.md)
- [XGameStreamingVideoFlags](/zh-CN/reference/system/xgamestreaming/enums/xgamestreamingvideoflags.md)
