image thumbnail

ScreenCapture - screenshot of component, figure or screen

version 1.18.0.0 (82.9 KB) by Yair Altman
ScreenCapture gets a screen-capture image of any Matlab GUI handle, or specified screen area rectangle

15.5K Downloads

Updated 21 Jul 2021

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

ScreenCapture gets a screen-capture of any Matlab GUI handle (including desktop, figure, axes, image, or uicontrol), or a specified area rectangle located relative to the specified handle.
Screen area capture is possible by specifying the root (desktop) handle (=0).
The output can be either to an image file or to a Matlab matrix (useful for displaying via imshow() or for further processing), or to the system clipboard.
This utility also enables adding a toolbar button for easy interactive screen-capture.
Syntax:
imageData = screencapture(handle, position, target, 'PropName',PropValue, ...)
Input Parameters:
handle - optional handle to be used for screen-capture origin. If empty/unsupplied then current figure (gcf) will be used.
position - optional position array in pixels: [x,y,width,height].
If empty or non-specified then the handle's position vector will be used.
If both handle and position are empty or non-specified then the position will be retrieved via interactive mouse-selection.
If handle is an image, then position is in data (not pixel) units, so the captured region remains the same after figure/axes resize (like imcrop)
target - optional filename for storing the screen-capture, or the 'clipboard'/'printer' string.
If empty or non-specified then no output to file will be done.
The file format will be determined from its extension (JPG/PNG/...).
Supported formats are those supported by the imwrite function.
If neither target nor imageData were specified, the user will be asked to interactively specify the output file.
'PropName',PropValue -
optional list of property pairs e.g., screencapture('target','sc.png','pos',[10,20,30,40],'handle',gca)
PropNames may be abbreviated and are case-insensitive.
PropNames may also be given in whichever order.
Supported PropNames are:
- 'handle' (default: gcf handle)
- 'position' (default: gcf position array)
- 'target' (default: '')
- 'toolbar' (figure handle; default: gcf) this adds a screen-capture button to the figure's toolbar. If this parameter is specified, then no screen-capture will take place and the returned imageData will be [].
Output parameters:
imageData - image data in a format acceptable by the imshow function. If neither filename nor imageData were specified, the user will be asked to interactively specify the output file.
Examples:
imageData = screencapture; % interactively select screen-capture rectangle
imageData = screencapture(hListbox); % capture image of a uicontrol
imageData = screencapture(0); % capture image of entire screen
imageData = screencapture(0, [20,30,40,50]); % select a small desktop region
imageData = screencapture(gcf,[20,30,40,50]); % select a small figure region
imageData = screencapture(gca,[10,20,30,40]); % select a small axes region
imshow(imageData); % display the captured image in a matlab figure
imwrite(imageData,'myImage.png'); % save the captured image to file
screencapture(gcf,[],'myFigure.jpg'); % capture the entire figure into file
screencapture(gcf,[],'clipboard'); % capture the entire figure into clipboard
screencapture(gcf,[],'printer'); % print the entire figure
screencapture('handle',gcf, 'target','myFigure.jpg'); % same as previous, save to file
screencapture('handle',gcf, 'target','clipboard'); % same as previous, copy to system clipboard
screencapture('handle',gcf, 'target','printer'); % same as previous, send to printer
screencapture('toolbar',gcf); % adds a screen-capture button to gcf's toolbar
screencapture('toolbar',[], 'target','sc.bmp'); % same with default output filename
Technical description:
https://UndocumentedMatlab.com/articles/screencapture-utility
Bugs and suggestions:
Please send to Yair Altman (altmany at gmail dot com)

Cite As

Yair Altman (2021). ScreenCapture - screenshot of component, figure or screen (https://www.mathworks.com/matlabcentral/fileexchange/24323-screencapture-screenshot-of-component-figure-or-screen), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2007b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!