Java Image Save writeImageIconToFile(ImageIcon icon, String imageType, File targetFile)

Here you can find the source of writeImageIconToFile(ImageIcon icon, String imageType, File targetFile)

Description

Writes the given image icon to the given file in the given imageType.

License

Open Source License

Parameter

Parameter Description
icon The icon to write.
imageType The image type.
targetFile The file to write the image to.

Return

True if the file could be written, false if something went wrong.

Declaration

public static boolean writeImageIconToFile(ImageIcon icon, String imageType, File targetFile) 

Method Source Code


//package com.java2s;
/*/*from w  w w . j  av a 2  s . c  o m*/
 * TV-Browser
 * Copyright (C) 04-2003 Martin Oberhauser (martin_oat@yahoo.de)
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * CVS information:
 *  $RCSfile$
 *   $Source$
 *     $Date: 2010-11-21 15:38:33 +0100 (Sun, 21 Nov 2010) $
 *   $Author: bananeweizen $
 * $Revision: 6835 $
 */

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class Main {
    /**
     * Writes the given image icon to the given file in the given imageType.
     *
     * @param icon The icon to write.
     * @param imageType The image type.
     * @param targetFile The file to write the image to.
     *
     * @return <code>True</code> if the file could be written, <code>false</code> if something went wrong.
     * @since 2.6
     */
    public static boolean writeImageIconToFile(ImageIcon icon, String imageType, File targetFile) {
        try {
            BufferedImage iconimage = new BufferedImage(icon.getIconWidth(), icon.getIconWidth(),
                    BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = iconimage.createGraphics();
            icon.paintIcon(null, g2, 0, 0);
            g2.dispose();

            ImageIO.write(iconimage, imageType, targetFile);
        } catch (Exception e) {
            return false;
        }

        return true;
    }
}

Related

  1. write(Image image, ImageOutputStream ios, ImageWriter writer)
  2. writeImageToBlob(int column, PreparedStatement psmt, Image image)