我不应该得到这个功劳,因为这是从MATLAB Answers中的一个答案。
https://www.mathworks.com/matlabcentral/answers/410919-capturing-and-saving-an-image-of-a-running-using-code-in-matlab-s-appdesigner
代码:
robot = java.awt.Robot();
pos = [0 0 1680 1050];
rect = java.awt.Rectangle(pos(1),pos(2),pos(3),pos(4));
cap = robot.createScreenCapture(rect);
rgb = typecast(cap.getRGB(0,0,cap.getWidth,cap.getHeight,[],0,cap.getWidth),'uint8');
imgData = zeros(cap.getHeight,cap.getWidth,3,'uint8');
imgData(:,:,1) = reshape(rgb(3:4:end),cap.getWidth,[])';
imgData(:,:,2) = reshape(rgb(2:4:end),cap.getWidth,[])';
imgData(:,:,3) = reshape(rgb(1:4:end),cap.getWidth,[])';
imshow(imgData)
imwrite(imgData,'I:/screenCap.tif', 'Resolution', 500)
这是一个非常好用的屏幕截图选项,只需将窗口最大化即可。