1# @ohos.screenshot (Screenshot) 2 3The **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. 4 5> **NOTE** 6> 7> 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. 8> 9> The APIs provided by this module are system APIs. 10 11## Modules to Import 12 13```ts 14import screenshot from '@ohos.screenshot'; 15``` 16 17## ScreenshotOptions 18 19Describes screenshot options. 20 21**System capability**: SystemCapability.WindowManager.WindowManager.Core 22 23 24| Name | Type | Mandatory| Description | 25| ---------------------- | ------------- | ---- | ------------------------------------------------------------ | 26| screenRect | [Rect](#rect) | No | Region of the screen to capture. If this parameter is null, the full screen will be captured. | 27| imageSize | [Size](#size) | No | Size of the screen region to capture. If this parameter is null, the full screen will be captured. | 28| 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. | 29| 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.| 30 31 32## Rect 33 34Describes the region of the screen to capture. 35 36**System capability**: SystemCapability.WindowManager.WindowManager.Core 37 38| Name| Type | Mandatory| Description | 39| ------ | ------ | ---- | ------------------------------------------------------------ | 40| left | number | Yes | Left boundary of the screen region to capture, in pixels. The value must be an integer.| 41| top | number | Yes | Top boundary of the screen region to capture, in pixels. The value must be an integer.| 42| width | number | Yes | Width of the screen region to capture, in pixels. The value must be an integer.| 43| height | number | Yes | Height of the screen region to capture, in pixels. The value must be an integer.| 44 45 46## Size 47 48Describes the size of the screen region to capture. 49 50**System capability**: SystemCapability.WindowManager.WindowManager.Core 51 52| Name| Type | Mandatory| Description | 53| ------ | ------ | ---- | ------------------------------------------------------------ | 54| width | number | Yes | Width of the screen region to capture, in pixels. The value must be an integer.| 55| height | number | Yes | Height of the screen region to capture, in pixels. The value must be an integer.| 56 57## screenshot.save 58 59save(options: ScreenshotOptions, callback: AsyncCallback<image.PixelMap>): void 60 61Takes a screenshot and saves it as a **PixelMap** object. This API uses an asynchronous callback to return the result. 62 63**System capability**: SystemCapability.WindowManager.WindowManager.Core 64 65**Required permissions**: ohos.permission.CAPTURE_SCREEN (available only to system applications) 66 67**Parameters** 68 69| Name | Type | Mandatory| Description | 70| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 71| options | [ScreenshotOptions](#screenshotoptions) | Yes | Screenshot settings consist of **screenRect**, **imageSize**, **rotation**, and **displayId**. You can set the parameters separately.| 72| callback | AsyncCallback<[image.PixelMap](js-apis-image.md#pixelmap7)> | Yes | Callback used to return a **PixelMap** object. | 73 74**Example** 75 76```ts 77import { BusinessError } from '@ohos.base'; 78 79let screenshotOptions: screenshot.ScreenshotOptions = { 80 "screenRect": { 81 "left": 200, 82 "top": 100, 83 "width": 200, 84 "height": 200 }, 85 "imageSize": { 86 "width": 300, 87 "height": 300 }, 88 "rotation": 0, 89 "displayId": 0 90}; 91try { 92 screenshot.save(screenshotOptions, (err: BusinessError, pixelMap) => { 93 if (err) { 94 console.log('Failed to save screenshot. Code: ' + JSON.stringify(err)); 95 return; 96 } 97 console.log('Succeeded in saving sreenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); 98 pixelMap.release(); // Release the memory in time after the PixelMap is used. 99 }); 100} catch (exception) { 101 console.error('Failed to save screenshot. Code: ' + JSON.stringify(exception)); 102}; 103``` 104 105## screenshot.save 106 107save(callback: AsyncCallback<image.PixelMap>): void 108 109Takes a screenshot and saves it as a **PixelMap** object. This API uses an asynchronous callback to return the result. 110 111**System capability**: SystemCapability.WindowManager.WindowManager.Core 112 113**Required permissions**: ohos.permission.CAPTURE_SCREEN (available only to system applications) 114 115**Parameters** 116 117| Name | Type | Mandatory| Description | 118| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 119| callback | AsyncCallback<[image.PixelMap](js-apis-image.md#pixelmap7)> | Yes | Callback used to return a **PixelMap** object. | 120 121**Example** 122 123```ts 124import { BusinessError } from '@ohos.base'; 125 126try { 127 screenshot.save((err: BusinessError, pixelMap) => { 128 if (err) { 129 console.log('Failed to save screenshot. Code: ' + JSON.stringify(err)); 130 return; 131 } 132 console.log('Succeeded in saving sreenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); 133 pixelMap.release(); // Release the memory in time after the PixelMap is used. 134 }); 135} catch (exception) { 136 console.error('Failed to save screenshot. Code: ' + JSON.stringify(exception)); 137}; 138``` 139 140## screenshot.save 141 142save(options?: ScreenshotOptions): Promise<image.PixelMap> 143 144Takes a screenshot and saves it as a **PixelMap** object. This API uses a promise to return the result. 145 146**System capability**: SystemCapability.WindowManager.WindowManager.Core 147 148**Required permissions**: ohos.permission.CAPTURE_SCREEN (available only to system applications) 149 150**Parameters** 151 152| Name | Type | Mandatory| Description | 153| ------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 154| options | [ScreenshotOptions](#screenshotoptions) | No | Screenshot settings consist of **screenRect**, **imageSize**, **rotation**, and **displayId**. You can set the parameters separately.| 155 156**Return value** 157 158| Type | Description | 159| ----------------------------- | ----------------------------------------------- | 160| Promise<[image.PixelMap](js-apis-image.md#pixelmap7)> | Promise used to return a **PixelMap** object.| 161 162**Example** 163 164```ts 165import { BusinessError } from '@ohos.base'; 166 167let screenshotOptions: screenshot.ScreenshotOptions = { 168 "screenRect": { 169 "left": 200, 170 "top": 100, 171 "width": 200, 172 "height": 200 }, 173 "imageSize": { 174 "width": 300, 175 "height": 300 }, 176 "rotation": 0, 177 "displayId": 0 178}; 179try { 180 let promise = screenshot.save(screenshotOptions); 181 promise.then((pixelMap) => { 182 console.log('Succeeded in saving sreenshot. Pixel bytes number: ' + pixelMap.getPixelBytesNumber()); 183 pixelMap.release(); // Release the memory in time after the PixelMap is used. 184 }).catch((err: BusinessError) => { 185 console.log('Failed to save screenshot. Code: ' + JSON.stringify(err)); 186 }); 187} catch (exception) { 188 console.error('Failed to save screenshot. Code: ' + JSON.stringify(exception)); 189}; 190``` 191