> ## 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.

# XUserGetGamerPictureResultSize

> XUserGetGamerPictureResultSize

# XUserGetGamerPictureResultSize

返回 [XUserGetGamerPictureResult](/reference/system/xuser/functions/xusergetgamerpictureresult) 检索 [XUserGetGamerPictureAsync](/reference/system/xuser/functions/xusergetgamerpictureasync) 返回的玩家图片所需的缓冲区大小。

## 语法

```cpp theme={null}
HRESULT XUserGetGamerPictureResultSize(  
         XAsyncBlock* async,  
         size_t* bufferSize  
)  
```

### 参数

*async*   \_Inout\_\
类型：[XAsyncBlock\*](/reference/system/xasync/structs/xasyncblock)

发送给 [XUserGetGamerPictureAsync](/reference/system/xuser/functions/xusergetgamerpictureasync) 的异步块。

*bufferSize*   \_Out\_\
类型：size\_t\*

包含缓冲区大小要求。

### 返回值

类型：HRESULT

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

## 注解

若要以异步方式检索特定用户的玩家图片，请调用 [XUserGetGamerPictureAsync](/reference/system/xuser/functions/xusergetgamerpictureasync)。

若要检索调用 [XUserGetGamerPictureAsync](/reference/system/xuser/functions/xusergetgamerpictureasync) 的结果，请调用 [XUserGetGamerPictureResult](/reference/system/xuser/functions/xusergetgamerpictureresult)。

以下示例演示如何加载玩家图片。

```cpp theme={null}
HRESULT LoadGamerPicComplete(XAsyncBlock* abResult)
{
    try
    {
        struct CreateTextureContext
        {
            User* This;
            ImTextureID TextureId;
            std::vector<unsigned char> GamerpicBuffer;
        };

        auto context = std::make_unique<CreateTextureContext>();
        context->This = this;
        context->TextureId = IMTEXTUREID_INVALID;

        // Get the buffer size and set up a buffer to contain it
        size_t bufferSize;
        RETURN_IF_FAILED(XUserGetGamerPictureResultSize(abResult, &bufferSize));
        context->GamerpicBuffer.resize(bufferSize);

        size_t bufferUsed;
        RETURN_IF_FAILED(XUserGetGamerPictureResult(
            abResult,
            context->GamerpicBuffer.size(),
            context->GamerpicBuffer.data(),
            &bufferUsed));
        FAIL_FAST_HR_IF(E_UNEXPECTED, bufferSize != bufferUsed);

        // Create some async work to create a dx texture off the UI thread
        // and then set the texture id back on the UI thread
        auto abCreateTexture = std::make_unique<XAsyncBlock>();
        ZeroMemory(abCreateTexture.get(), sizeof(*abCreateTexture));
        abCreateTexture->queue = abResult->queue;
        abCreateTexture->context = context.get();

        // Set the texture id in the completion on the UI thread
        abCreateTexture->callback = [](XAsyncBlock* ab)
        {
            auto context = static_cast<CreateTextureContext*>(ab->context);
            context->This->_textureId = context->TextureId;
            delete context;
            delete ab;
        };

        // Worker will create the texture on a work thread
        auto createTextureWorker = [](XAsyncBlock* ab) -> HRESULT
        {
            auto context = static_cast<CreateTextureContext*>(ab->context);
            auto dimension = GetDimensionFromSize(GamerPicSize);
            RETURN_IF_FAILED(CreateTextureFromPng(context->GamerpicBuffer.data(), context->GamerpicBuffer.size(), &context->TextureId));
            return S_OK;
        };

        // Create the texture on a work thread
        if (SUCCEEDED_LOG(XAsyncRun(abCreateTexture.get(), createTextureWorker)))
        {
            context.release();
            abCreateTexture.release();
        }
    }
    CATCH_RETURN();
    return S_OK;
}

HRESULT LoadGamerPicAsync(XTaskQueueHandle queue, uint32_t cancelTime = 0)
{
    auto asyncBlock = std::make_shared<XAsyncBlock>();
    ZeroMemory(asyncBlock.get(), sizeof(*asyncBlock));
    asyncBlock->queue = queue;

    struct GamerPicContext
    {
        User* User;
        std::shared_ptr<XAsyncBlock> AsyncBlock;
    };

    auto context = std::make_unique<GamerPicContext>();
    context->User = this;
    context->AsyncBlock = asyncBlock;
    asyncBlock->context = context.get();

    asyncBlock->callback = [](XAsyncBlock* ab)
    {
        std::unique_ptr<GamerPicContext> context(reinterpret_cast<GamerPicContext*>(ab->context));
        LOG_IF_FAILED(context->User->LoadGamerPicComplete(ab));
    };

    if (cancelTime != 0)
    {
        std::thread cancelThread(
            [asyncBlock]()
        {
            std::this_thread::sleep_for(std::chrono::milliseconds(5000));
            XAsyncCancel(asyncBlock.get());
        });

        cancelThread.detach();
    }

    if (SUCCEEDED_LOG(XUserGetGamerPictureAsync(_handle.get(), GamerPicSize, asyncBlock.get())))
    {
        context.release();
    }

    return S_OK;
}
```

## 要求

**头文件：** XUser.h

**库：** xgameruntime.lib

**支持的平台：** Windows、Steam Deck、XBOX One 系列主机和 XBOX Series 主机

## 另请参阅

[XUser](/reference/system/xuser/xuser_members)

[XUserGetGamerPictureAsync](/reference/system/xuser/functions/xusergetgamerpictureasync)

[XUserGetGamerPictureResult](/reference/system/xuser/functions/xusergetgamerpictureresult)


## Related topics

- [XUser](/zh-CN/reference/system/xuser/xuser_members.md)
- [面向 GDK 的 Unity C# API 包装器](/zh-CN/build/gdk-and-engines/unity/unity-api-wrappers.md)
- [XUserGamerPictureSize](/zh-CN/reference/system/xuser/enums/xusergamerpicturesize.md)
- [XUserGetTokenAndSignatureResultSize](/zh-CN/reference/system/xuser/functions/xusergettokenandsignatureresultsize.md)
- [XblUserStatisticsGetMultipleUserStatisticsResultSize](/zh-CN/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsgetmultipleuserstatisticsresultsize.md)
