If you have root or even shell permission, you can use either screenshot or screencap command, for your reference:
root@android:/system/bin # screenshot -h
unknown option -- husage: screenshot [-s soundfile] filename.png
-s: play a sound effect to signal success
-i: autoincrement to avoid overwriting filename.png
root@android:/system/bin # screencap -h
usage: screencap [-hp] [-d display-id] [FILENAME]
-h: this message
-p: save the file as a png.
-d: specify the display id to capture, default 0.
If FILENAME ends with .png it will be saved as a png.
If FILENAME is not given, the results will be printed to stdout.
If your device does not have these program in native, you can simply write one by yourself. The source code is here: https://android.googlesource.com/platform/frameworks/base/+/master/cmds/screencap/screencap.cpp