Gui Screen Shot Button
3 次查看(过去 30 天)
显示 更早的评论
Hello all,
I have a GUI that i created using guide and I am trying to create a push button that can be used to save a screen shot of the GUI window. I want to be able to select the location it is saving to and have it same with the same resolution. I got it working using the saveas function, but it saves, but the images come out very low quality. The save as code is as follows:
function pushbutton1_Callback(hObject, eventdata, handles)
[name,path,index] = uiputfile
saveas(Sections,[path name],'jpg');
I am not sure if there is a way to do this with the print command or not, but any help is appreciated.
1 个评论
Geoff Hayes
2014-11-10
Christopher - how are you creating Sections which is presumably the screen shot of your GUI? It could be that the resolution is low quality since you are saving this image to jpeg. Have you tried bmp or other formats?
采纳的回答
Image Analyst
2014-11-10
Try this snippet to get a valid filename.
% Note, if you're saving an image you can use imsave() instead of uiputfile().
startingFolder = userpath
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
3 个评论
Image Analyst
2014-11-13
Nothing - you don't use it. The first argument is the full file name - that's why I called it that. It's the drive, folder, basefilename, and extension all in one single string as the second argument. The handle is the first argument.
更多回答(1 个)
Kiarash Ahi
2018-3-3
编辑:Kiarash Ahi
2018-3-3
I put it like this, but did not work, could you please help me:
function pushbutton76_Callback(hObject, eventdata, handles)
% Note, if you're saving an image you can use imsave() instead of uiputfile().
startingFolder = userpath
defaultFileName = fullfile(startingFolder, 'scsh.jpg')
saveas(Sections,[path name],'jpg');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
2 个评论
Image Analyst
2018-3-4
Try this:
% Get the name of the file that the user wants to save.
% Note, if you're saving an image you can use imsave() instead of uiputfile().
startingFolder = pwd % Or "userpath" or wherever you want.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
saveas(Sections, fullFileName);
The thing though is that you need to get the variable Sections in to that function. For that, see the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
Kiarash Ahi
2018-3-4
I fixed it like this, it also keeps the resolution to what you see on your screen:
function pushbutton76_Callback(hObject, eventdata, handles)
% Note, if you're saving an image you can use imsave() instead of uiputfile().
matlab.graphics.internal.setPrintPreferences('DefaultPaperPositionMode','auto')
CHEA=handles.CHEA;
fig = gcf;
fig.PaperPositionMode = 'auto';
print('ScreenSizeFigure','-dpng','-r0')
startingFolder = userpath
global num;
n=num;
%s
ch=char(CHEA(n,1))
n=num2str(n)
n=char(n)
ch=strcat(ch,'-',n,'.png')
%ch=fprintf('%s',CHEA(n,1))
defaultFileName = fullfile(startingFolder,ch)
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
saveas(gca,defaultFileName,'png');
fig.PaperPositionMode = 'auto';
print('ScreenSizeFigure','-dpng','-r0')
if baseFileName == 0
% User clicked the Cancel button.
return;
end
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!