ScreenCapture - screenshot of component, figure or screen

ScreenCapture gets a screen-capture image of any Matlab GUI handle, or specified screen area rectangle
Updated 4 Dec 2022

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.
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.
Usage 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:
Bugs and suggestions:
Please send to Yair Altman (altmany at gmail dot com)

Cite As

Yair Altman (2024). 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
Find more on Printing and Saving in Help Center and MATLAB Answers
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes

Fixed clipboard export to support transparent and gray-scale images

added copyright reference to imclipboard

Fixed clipboard capture on R2022a onward (reported by John Mullins)

Fixed Y coordinates of full-screen capture; avoid needless figure focus

Fix annoying warning about JavaFrame property becoming obsolete someday (yes, we know...)

Fix for deployes application suggested by David Bartholomew

Added the 'printer' target

Fix for saving GIF files

Fixes for R2014b

Fixed bug when capturing interactive selection; only enable image formats when saving to an unspecified file via uiputfile

Fixed bug in capture of non-square image; fixes for Win64

(a more prominent acknowledgement of Jiro's imclipboard tool on the FEX page) - no changes to the actual files

FEX page credit to Jiro Doke's imclipboard utility

Fixed capture of Desktop (root); enabled rbbox anywhere on desktop (not necesarily in a Matlab figure); enabled output to clipboard (based on Jiro Doke's imclipboard utility); edge-case fixes; added Java compatibility check

Capture current object if w=h=0 (clicking a single point); extra input args sanity checks; fix for docked windows; include axes labels & ticks by default when capturing axes; use data-units position vector when capturing images; many edge-case fixes

another performance boost (thanks to Jan Simon); some compatibility fixes for Matlab 6.5 (untested)

updated attached screenshot with explanatory notes

Handle missing output format; performance boost (thanks to Urs); fix minor root-handle bug; added toolbar button option