Java Image saveComponentAsImage(final String path, final Component target, final String format)

Here you can find the source of saveComponentAsImage(final String path, final Component target, final String format)

Description

Given a component, saves it on disk as image.

License

Apache License

Parameter

Parameter Description
path the file to save
target the component to save as image
format a string describing the format. See http://docs.oracle.com/javase /7/docs/api/javax/imageio/package- summary.html#package_description for a list of available formats.

Exception

Parameter Description
IOException in case of I/O Errors

Declaration

public static void saveComponentAsImage(final String path, final Component target, final String format)
        throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2009, 2015, Danilo Pianini and contributors
 * listed in the project's build.gradle or pom.xml file.
 *
 * This file is distributed under the terms of the Apache License, version 2.0
 *******************************************************************************/

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.SwingUtilities;

public class Main {
    /**/* ww w  .  ja  va 2  s .  c om*/
     * Given a component, saves it on disk as image.
     * 
     * @param path
     *            the file to save
     * @param target
     *            the component to save as image
     * @param format
     *            a string describing the format. See
     *            http://docs.oracle.com/javase
     *            /7/docs/api/javax/imageio/package-
     *            summary.html#package_description for a list of available
     *            formats.
     * @throws IOException
     *             in case of I/O Errors
     */
    public static void saveComponentAsImage(final String path, final Component target, final String format)
            throws IOException {
        final JFrame win = (JFrame) SwingUtilities.getWindowAncestor(target);
        final Dimension size = win.getSize();
        final BufferedImage image = (BufferedImage) win.createImage(size.width, size.height);
        final Graphics g = image.getGraphics();
        win.paint(g);
        g.dispose();
        ImageIO.write(image, format, new File(path));
    }
}

Related

  1. prepareImagePath(String html, URL url)
  2. removeImage(JLabel image)
  3. removeRedeye(Image im, final int x1, final int y1, final int x2, final int y2)
  4. renderComponentToImage(JComponent component)
  5. save(Image image, File file, String formatName)
  6. SaveImageToFile(Image img, String filename, String outformat)
  7. scaleBarcodeMaxWidth(Image i, int width, int cropHeigth)
  8. setImage(Image im, JComponent c)
  9. setImageDimension(Image imagen)