Java JComponent to Image doScreenshotToFile(final Component container, final Path filePath, final String imageType)

Here you can find the source of doScreenshotToFile(final Component container, final Path filePath, final String imageType)

Description

do Screenshot To File

License

Open Source License

Declaration

public static File doScreenshotToFile(final Component container, final Path filePath, final String imageType)
            throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Component;

import java.awt.GraphicsConfiguration;

import java.awt.GraphicsEnvironment;

import java.awt.Rectangle;

import java.awt.Transparency;
import java.awt.image.BufferedImage;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import javax.imageio.ImageIO;

public class Main {
    private final static GraphicsConfiguration GC = (GraphicsEnvironment.isHeadless() == false)
            ? GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration()
            : null;// w  w  w .j a  v  a  2s . c om

    public static File doScreenshotToFile(final Component container, final Path filePath, final String imageType)
            throws IOException {
        final File imageFile = new File(filePath.toString());
        ImageIO.write(getScreenshotImage(container), imageType, imageFile);
        return imageFile;
    }

    /**
     * Creates an image of the contents of container and saves to file.
     */
    public static File doScreenshotToFile(final Component container, final Path filePath) throws IOException {
        return doScreenshotToFile(container, filePath, "png");
    }

    private static BufferedImage getScreenshotImage(final Component container) {
        final Rectangle rec = container.getBounds();
        final BufferedImage capture = getCompatibleBufferedImage(rec.width, rec.height);
        container.paint(capture.getGraphics());
        return capture;
    }

    public static BufferedImage getCompatibleBufferedImage(final int width, final int height,
            final int transparency) {
        if (GC != null) {
            return GC.createCompatibleImage(width, height, transparency);
        } else {
            final int type = (transparency == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
                    : BufferedImage.TYPE_INT_ARGB;
            return new BufferedImage(width, height, type);
        }
    }

    public static BufferedImage getCompatibleBufferedImage(final int width, final int height) {
        return getCompatibleBufferedImage(width, height, Transparency.OPAQUE);
    }

    public static BufferedImage getCompatibleBufferedImage(BufferedImage image) {
        return getCompatibleBufferedImage(image.getWidth(), image.getHeight(), image.getTransparency());
    }
}

Related

  1. buildImage(Component c)
  2. componentToImage(Component c)
  3. componentToImage(Component c)
  4. componentToImage(Component component, int resolution)
  5. ensureImageLoaded(Component owner, Image image)
  6. findConnectedComponents(int[][] image)
  7. generateImageFileFromComponent(Component component, String filename, String Type)
  8. setCustomCursor(Component component, Image cursorImg, String name)