1# Snapshot 2 3Snapshot provides APIs to take snapshot image for LVGL object together with its children. The image will look exactly like the object. 4 5## Usage 6 7Simply call API `lv_snapshot_take` to generate the image descriptor which can be set as image object src using `lv_img_set_src`. 8 9 10Note, only below color formats are supported for now: 11 - LV_IMG_CF_TRUE_COLOR_ALPHA 12 - LV_IMG_CF_ALPHA_1BIT 13 - LV_IMG_CF_ALPHA_2BIT 14 - LV_IMG_CF_ALPHA_4BIT 15 - LV_IMG_CF_ALPHA_8BIT 16 17 18### Free the Image 19The memory `lv_snapshot_take` uses are dynamically allocated using `lv_mem_alloc`. Use API `lv_snapshot_free` to free the memory it takes. This will firstly free memory the image data takes, then the image descriptor. 20 21 22Take caution to free the snapshot but not delete the image object. Before free the memory, be sure to firstly unlink it from image object, using `lv_img_set_src(NULL)` and `lv_img_cache_invalidate_src(src)`. 23 24 25Below code snippet explains usage of this API. 26 27```c 28void update_snapshot(lv_obj_t * obj, lv_obj_t * img_snapshot) 29{ 30 lv_img_dsc_t* snapshot = (void*)lv_img_get_src(img_snapshot); 31 if(snapshot) { 32 lv_snapshot_free(snapshot); 33 } 34 snapshot = lv_snapshot_take(obj, LV_IMG_CF_TRUE_COLOR_ALPHA); 35 lv_img_set_src(img_snapshot, snapshot); 36} 37``` 38 39### Use Existing Buffer 40If the snapshot needs update now and then, or simply caller provides memory, use API `lv_res_t lv_snapshot_take_to_buf(lv_obj_t * obj, lv_img_cf_t cf, lv_img_dsc_t * dsc, void * buf, uint32_t buf_size);` for this case. It's caller's responsibility to alloc/free the memory. 41 42 43If snapshot is generated successfully, the image descriptor is updated and image data will be stored to provided `buf`. 44 45 46Note that snapshot may fail if provided buffer is not enough, which may happen when object size changes. It's recommended to use API `lv_snapshot_buf_size_needed` to check the needed buffer size in byte firstly and resize the buffer accordingly. 47 48## Example 49 50```eval_rst 51 52.. include:: ../../examples/others/snapshot/index.rst 53 54``` 55## API 56 57 58```eval_rst 59 60.. doxygenfile:: lv_snapshot.h 61 :project: lvgl 62 63``` 64