Java BufferedImage Save saveImage(BufferedImage img, String extension)

Here you can find the source of saveImage(BufferedImage img, String extension)

Description

Asks the user for a file and then saves the image to that file.

License

GNU General Public License

Parameter

Parameter Description
img The image to save to file
extension The format of the image (e.g. "png", "jpg", or "gif", see <code>javax.imageio.ImageIO.write(...)</code>.

Exception

Parameter Description
IOException thrown if the file cannot be written to (typically caused by read/write permission conflicts).

Declaration

public static void saveImage(BufferedImage img, String extension) throws IOException 

Method Source Code

//package com.java2s;
/*//from ww w . j  av  a 2s. c o m
CCH World Factory - GPL
    
Copyright (C) 2014 Christopher Collin Hall
email: explosivegnome@yahoo.com
    
CCH World Factory - GPL is distributed under the GNU General Public 
License (GPL) version 3. A non-GPL branch of the CCH World Factory 
also exists. For non-GPL licensing options, contact the copyright 
holder, Christopher Collin Hall (explosivegnome@yahoo.com). 
    
CCH World Factory - GPL is free software: you can redistribute it 
and/or modify it under the terms of the GNU General Public License 
as published by the Free Software Foundation, either version 3 of 
the License, or (at your option) any later version.
    
CCH World Factory - GPL is distributed in the hope that it will be 
useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with CCH World Factory - GPL.  If not, see 
<http://www.gnu.org/licenses/>.
    
*/

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

import javax.swing.JFileChooser;

import javax.swing.JOptionPane;

public class Main {
    /**
     * Asks the user for a file and then saves the image to that file.
     * @param img The image to save to file
     * @param extension The format of the image (e.g. "png", "jpg", or "gif", 
     * see <code>javax.imageio.ImageIO.write(...)</code>.
     * @throws IOException thrown if the file cannot be written to (typically 
     * caused by read/write permission conflicts).
     */
    public static void saveImage(BufferedImage img, String extension) throws IOException {
        JFileChooser fc = new JFileChooser(".");
        int action = fc.showSaveDialog(null);
        if (action == JFileChooser.APPROVE_OPTION && fc.getSelectedFile() != null) {
            File f = fc.getSelectedFile();
            if (f.getPath().toLowerCase().endsWith("." + extension.toLowerCase()) == false) {
                f = new File(f.getPath() + "." + extension);
            }
            if (f.exists()) {
                int click = JOptionPane.showConfirmDialog(null,
                        "File '" + f.getPath() + "' already exists. Replace it?");
                if (click == JOptionPane.CANCEL_OPTION) {
                    return;
                }
            }
            ImageIO.write(img, extension, f);
        }
    }
}

Related

  1. saveImage(BufferedImage image)
  2. saveImage(BufferedImage img, String targetfile)
  3. SaveImageToFile(BufferedImage bimg, String filename, String outformat)