Java Image to BufferedImage bufferedImageFromImage(final Image image)

Here you can find the source of bufferedImageFromImage(final Image image)

Description

Creates a BufferedImage from an Image .

License

Open Source License

Parameter

Parameter Description
image An Image to be converted to a BufferedImage.

Return

A BufferedImage instance of the same given image.

Declaration

public static BufferedImage bufferedImageFromImage(final Image image) 

Method Source Code


//package com.java2s;

import java.awt.Graphics2D;
import java.awt.Image;

import java.awt.image.BufferedImage;

public class Main {
    /**/*  w ww  .  j a va 2 s  .c  om*/
     * Creates a {@link BufferedImage} from an {@link Image}.
     *
     * @param image An Image to be converted to a BufferedImage.
     * @return      A BufferedImage instance of the same given image.
     */
    public static BufferedImage bufferedImageFromImage(final Image image) {
        if (image instanceof BufferedImage) {
            return (BufferedImage) image;
        }

        final BufferedImage out = new BufferedImage(image.getWidth(null), image.getHeight(null),
                BufferedImage.TYPE_INT_ARGB);
        final Graphics2D g2d = out.createGraphics();
        g2d.drawImage(image, 0, 0, null);
        g2d.dispose();
        return out;
    }
}

Related

  1. convertToBufferedImage(Image image)
  2. getBufferedImage(Image image)
  3. getBufferedImage(Image image)
  4. getBufferedImage(Image image)