1e41f4b71Sopenharmony_ci# @ohos.screenshot (Screenshot) (System API) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **Screenshot** module provides APIs for you to set information such as the region to capture and the size of the screen region when capturing a screen. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci> This topic describes only system APIs provided by the module. For details about its public APIs, see [@ohos.screenshot](js-apis-screenshot.md). 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## Modules to Import 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci```ts 13e41f4b71Sopenharmony_ciimport { screenshot } from '@kit.ArkUI'; 14e41f4b71Sopenharmony_ci``` 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## ScreenshotOptions 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ciDescribes screenshot options. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci**System API**: This is a system API. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci**System capability**: SystemCapability.WindowManager.WindowManager.Core 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 25e41f4b71Sopenharmony_ci| ---------------------- | ------------- | ---- | ------------------------------------------------------------ | 26e41f4b71Sopenharmony_ci| screenRect | [Rect](js-apis-screenshot.md#rect) | No | Region of the screen to capture. If this parameter is null, the full screen will be captured. | 27e41f4b71Sopenharmony_ci| imageSize | [Size](#size) | No | Size of the screen region to capture. If this parameter is null, the full screen will be captured. | 28e41f4b71Sopenharmony_ci| rotation | number | No | Rotation angle of the screenshot. Currently, the value can be **0** only. The default value is **0**. The value must be an integer. | 29e41f4b71Sopenharmony_ci| displayId<sup>8+</sup> | number | No | ID of the [display](js-apis-display.md#display) device on which the screen region is to be captured. The value must be an integer.| 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci## Size 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciDescribes the size of the screen region to capture. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci**System API**: This is a system API. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci**System capability**: SystemCapability.WindowManager.WindowManager.Core 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 40e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 41e41f4b71Sopenharmony_ci| width | number | Yes | Width of the screen region to capture, in px. The value must be an integer.| 42e41f4b71Sopenharmony_ci| height | number | Yes | Height of the screen region to capture, in px. The value must be an integer.| 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci## screenshot.save 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_cisave(options: ScreenshotOptions, callback: AsyncCallback<image.PixelMap>): void 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ciTakes a screenshot and saves it as a **PixelMap** object. This API uses an asynchronous callback to return the result. 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci**System API**: This is a system API. 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci**System capability**: SystemCapability.WindowManager.WindowManager.Core 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.CAPTURE_SCREEN (available only to system applications) 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci**Parameters** 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 59e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 60e41f4b71Sopenharmony_ci| options | [ScreenshotOptions](#screenshotoptions) | Yes | Screenshot settings consist of **screenRect**, **imageSize**, **rotation**, and **displayId**. You can set the parameters separately.| 61e41f4b71Sopenharmony_ci| callback | AsyncCallback<[image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)> | Yes | Callback used to return a **PixelMap** object. | 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci**Error codes** 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Display Error Codes](errorcode-display.md). 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci| ID| Error Message| 68e41f4b71Sopenharmony_ci| ------- | -------------------------- | 69e41f4b71Sopenharmony_ci| 201 | Permission verification failed.| 70e41f4b71Sopenharmony_ci| 202 | Permission verification failed. A non-system application calls a system API.| 71e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.| 72e41f4b71Sopenharmony_ci| 1400001 | Invalid display or screen. | 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci**Example** 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci```ts 77e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 78e41f4b71Sopenharmony_ciimport { image } from '@kit.ImageKit'; 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_cilet screenshotOptions: screenshot.ScreenshotOptions = { 81e41f4b71Sopenharmony_ci "screenRect": { 82e41f4b71Sopenharmony_ci "left": 200, 83e41f4b71Sopenharmony_ci "top": 100, 84e41f4b71Sopenharmony_ci "width": 200, 85e41f4b71Sopenharmony_ci "height": 200 }, 86e41f4b71Sopenharmony_ci "imageSize": { 87e41f4b71Sopenharmony_ci "width": 300, 88e41f4b71Sopenharmony_ci "height": 300 }, 89e41f4b71Sopenharmony_ci "rotation": 0, 90e41f4b71Sopenharmony_ci "displayId": 0 91e41f4b71Sopenharmony_ci}; 92e41f4b71Sopenharmony_citry { 93e41f4b71Sopenharmony_ci screenshot.save(screenshotOptions, (err: BusinessError, pixelMap: image.PixelMap) => { 94e41f4b71Sopenharmony_ci if (err) { 95e41f4b71Sopenharmony_ci console.log('Failed to save screenshot. Code: ' + JSON.stringify(err)); 96e41f4b71Sopenharmony_ci return; 97e41f4b71Sopenharmony_ci } 98e41f4b71Sopenharmony_ci console.log('Succeeded in saving screenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); 99e41f4b71Sopenharmony_ci pixelMap.release(); // Release the memory in time after the PixelMap is used. 100e41f4b71Sopenharmony_ci }); 101e41f4b71Sopenharmony_ci} catch (exception) { 102e41f4b71Sopenharmony_ci console.error('Failed to save screenshot. Code: ' + JSON.stringify(exception)); 103e41f4b71Sopenharmony_ci}; 104e41f4b71Sopenharmony_ci``` 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci## screenshot.save 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_cisave(callback: AsyncCallback<image.PixelMap>): void 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ciTakes a screenshot and saves it as a **PixelMap** object. This API uses an asynchronous callback to return the result. 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci**System API**: This is a system API. 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci**System capability**: SystemCapability.WindowManager.WindowManager.Core 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.CAPTURE_SCREEN (available only to system applications) 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci**Parameters** 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 121e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 122e41f4b71Sopenharmony_ci| callback | AsyncCallback<[image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)> | Yes | Callback used to return a **PixelMap** object. | 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci**Error codes** 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci| ID| Error Message| 130e41f4b71Sopenharmony_ci| ------- | -------------------------- | 131e41f4b71Sopenharmony_ci| 201 | Permission verification failed.| 132e41f4b71Sopenharmony_ci| 202 | Permission verification failed. A non-system application calls a system API.| 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci**Example** 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci```ts 137e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 138e41f4b71Sopenharmony_ciimport { image } from '@kit.ImageKit'; 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_citry { 141e41f4b71Sopenharmony_ci screenshot.save((err: BusinessError, pixelMap: image.PixelMap) => { 142e41f4b71Sopenharmony_ci if (err) { 143e41f4b71Sopenharmony_ci console.log('Failed to save screenshot. Code: ' + JSON.stringify(err)); 144e41f4b71Sopenharmony_ci return; 145e41f4b71Sopenharmony_ci } 146e41f4b71Sopenharmony_ci console.log('Succeeded in saving screenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); 147e41f4b71Sopenharmony_ci pixelMap.release(); // Release the memory in time after the PixelMap is used. 148e41f4b71Sopenharmony_ci }); 149e41f4b71Sopenharmony_ci} catch (exception) { 150e41f4b71Sopenharmony_ci console.error('Failed to save screenshot. Code: ' + JSON.stringify(exception)); 151e41f4b71Sopenharmony_ci}; 152e41f4b71Sopenharmony_ci``` 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci## screenshot.save 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_cisave(options?: ScreenshotOptions): Promise<image.PixelMap> 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ciTakes a screenshot and saves it as a **PixelMap** object. This API uses a promise to return the result. 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci**System API**: This is a system API. 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci**System capability**: SystemCapability.WindowManager.WindowManager.Core 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.CAPTURE_SCREEN (available only to system applications) 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci**Parameters** 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 169e41f4b71Sopenharmony_ci| ------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 170e41f4b71Sopenharmony_ci| options | [ScreenshotOptions](#screenshotoptions) | No | Screenshot settings consist of **screenRect**, **imageSize**, **rotation**, and **displayId**. You can set the parameters separately.| 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci**Return value** 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci| Type | Description | 175e41f4b71Sopenharmony_ci| ----------------------------- | ----------------------------------------------- | 176e41f4b71Sopenharmony_ci| Promise<[image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)> | Promise used to return a **PixelMap** object.| 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci**Error codes** 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci| ID| Error Message| 184e41f4b71Sopenharmony_ci| ------- | -------------------------- | 185e41f4b71Sopenharmony_ci| 201 | Permission verification failed.| 186e41f4b71Sopenharmony_ci| 202 | Permission verification failed. A non-system application calls a system API.| 187e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.| 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci**Example** 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci```ts 192e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 193e41f4b71Sopenharmony_ciimport { image } from '@kit.ImageKit'; 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_cilet screenshotOptions: screenshot.ScreenshotOptions = { 196e41f4b71Sopenharmony_ci "screenRect": { 197e41f4b71Sopenharmony_ci "left": 200, 198e41f4b71Sopenharmony_ci "top": 100, 199e41f4b71Sopenharmony_ci "width": 200, 200e41f4b71Sopenharmony_ci "height": 200 }, 201e41f4b71Sopenharmony_ci "imageSize": { 202e41f4b71Sopenharmony_ci "width": 300, 203e41f4b71Sopenharmony_ci "height": 300 }, 204e41f4b71Sopenharmony_ci "rotation": 0, 205e41f4b71Sopenharmony_ci "displayId": 0 206e41f4b71Sopenharmony_ci}; 207e41f4b71Sopenharmony_citry { 208e41f4b71Sopenharmony_ci let promise = screenshot.save(screenshotOptions); 209e41f4b71Sopenharmony_ci promise.then((pixelMap: image.PixelMap) => { 210e41f4b71Sopenharmony_ci console.log('Succeeded in saving screenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); 211e41f4b71Sopenharmony_ci pixelMap.release(); // Release the memory in time after the PixelMap is used. 212e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 213e41f4b71Sopenharmony_ci console.log('Failed to save screenshot. Code: ' + JSON.stringify(err)); 214e41f4b71Sopenharmony_ci }); 215e41f4b71Sopenharmony_ci} catch (exception) { 216e41f4b71Sopenharmony_ci console.error('Failed to save screenshot. Code: ' + JSON.stringify(exception)); 217e41f4b71Sopenharmony_ci}; 218e41f4b71Sopenharmony_ci``` 219