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

# XAppCaptureOpenScreenshotStream

> XAppCaptureOpenScreenshotStream

# XAppCaptureOpenScreenshotStream

打开屏幕截图流。

## Syntax

```cpp theme={null}
HRESULT XAppCaptureOpenScreenshotStream(  
         const char* localId,  
         XAppCaptureScreenshotFormatFlag screenshotFormat,  
         XAppCaptureScreenshotStreamHandle* handle,  
         uint64_t* totalBytes  
)  
```

### Parameters

*localId*   \_In\_\
类型：char\*

调用 [XAppCaptureTakeScreenshot](/reference/system/xappcapture/functions/xappcapturetakescreenshot) 之后在 [XAppCaptureTakeScreenshotResult](/reference/system/xappcapture/structs/xappcapturetakescreenshotresult) 中返回的屏幕截图本地 ID。

*screenshotFormat*   \_In\_\
类型：[XAppCaptureScreenshotFormatFlag](/reference/system/xappcapture/enums/xappcapturescreenshotformatflag)

调用 [XAppCaptureTakeScreenshot](/reference/system/xappcapture/functions/xappcapturetakescreenshot) 之后在 [XAppCaptureTakeScreenshotResult](/reference/system/xappcapture/structs/xappcapturetakescreenshotresult) 中返回的屏幕截图格式标志。

*handle*   \_Out\_\
类型：XAppCaptureScreenshotStreamHandle\*

打开流后返回的屏幕截图流句柄。

*totalBytes*   \_Out\_opt\_\
类型：uint64\_t\*

流中字节的总数。

### Return value

类型：HRESULT

函数结果。

## Remarks

<Note>此函数在时间敏感线程上调用不安全。有关详细信息，请参阅[时间敏感线程](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)。</Note>

无法从游戏分区访问屏幕截图，因此必须打开流以检索用户拍摄的或通过 [XAppCaptureTakeScreenshot](/reference/system/xappcapture/functions/xappcapturetakescreenshot) 拍摄的屏幕截图。[XAppCaptureTakeScreenshot](/reference/system/xappcapture/functions/xappcapturetakescreenshot) 会在其结果中返回 *localID*，此函数会使用该 ID 来检索相应的屏幕截图。只要屏幕截图保留在磁盘上，*localID* 就有效。主机上用于存储屏幕截图的空间有限。分配的空间用完后，较旧的屏幕截图会被删除。这可能会导致打开屏幕截图的函数无法命中某个 localID。打开流后，可以使用 [XAppCaptureReadScreenShotStream](/reference/system/xappcapture/functions/xappcapturereadscreenshotstream) 读取屏幕截图。最后，每个流都需要使用 [XAppCaptureCloseScreenshotStream](/reference/system/xappcapture/functions/xappcaptureclosescreenshotstream) 关闭，以避免内存泄漏。

```cpp theme={null}
XAppCaptureTakeScreenshotResult takeScreenshotResult = {0};
XAppCaptureScreenshotStreamHandle handle = nullptr;
UINT64 totalBytesToRead = 0;
XAppCaptureScreenshotFormatFlag screenshotFormat = XAppCaptureScreenshotFormatFlag::SDR;
bool hdrAvailable = false;

/* ... obtain takeScreenshotResult with XAppCaptureTakeScreenshot. Refer to corresponding documentation ... */

hdrAvailable = static_cast<bool>(takeScreenshotResult.availableScreenshotFormats & XAppCaptureScreenshotFormatFlag::HDR);

/* Note: It is optional to obtain the HDR screenshot, if HDR is available. You will need to call XAppCaptureOpenScreenshotStream twice to obtain both SDR and HDR screenshots */
if (hdrAvailable)
{
    screenshotFormat = XAppCaptureScreenshotFormatFlag::HDR;
}

if (FAILED_LOG(XAppCaptureOpenScreenshotStream(takeScreenshotResult.localId, screenshotFormat, &handle, &totalBytesToRead)))
{
    return;
}

appLog.AddLog("%I64d bytes returned\n", totalBytesToRead);

/* You must always call XAppCaptureCloseScreenshotStream on an open XAppCaptureScreenshotStreamHandle to avoid a memory leak */
if (handle != nullptr)
{
    XAppCaptureCloseScreenshotStream(handle);
}

```

## Requirements

**头文件：** XAppCapture.h

**库：** xgameruntime.lib

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

## Conceptual documentation

* [时间敏感线程](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## See also

[GameDVR 概述](/build/console-features/game-dvr/gamedvr-broadcast)\
[XAppCapture Members](/reference/system/xappcapture/xappcapture_members)\
[XAppCaptureReadScreenShotStream](/reference/system/xappcapture/functions/xappcapturereadscreenshotstream)\
[XAppCaptureTakeScreenshot](/reference/system/xappcapture/functions/xappcapturetakescreenshot)\
[XAppCaptureCloseScreenshotStream](/reference/system/xappcapture/functions/xappcaptureclosescreenshotstream)


## Related topics

- [XAppCaptureScreenshotStream](/zh-CN/reference/system/xappcapture/structs/xappcapturescreenshotstream.md)
- [XAppCaptureReadScreenshotStream](/zh-CN/reference/system/xappcapture/functions/xappcapturereadscreenshotstream.md)
- [XAppCapture](/zh-CN/reference/system/xappcapture/xappcapture_members.md)
- [面向 GDK 的 Unity C# API 包装器](/zh-CN/build/gdk-and-engines/unity/unity-api-wrappers.md)
- [XAppCaptureCloseScreenshotStream](/zh-CN/reference/system/xappcapture/functions/xappcaptureclosescreenshotstream.md)
