Convert an Image into a TYPE_INT_ARGB BufferedImage. - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Convert

Description

Convert an Image into a TYPE_INT_ARGB BufferedImage.

Demo Code


//package com.java2s;
import java.awt.*;

import java.awt.image.*;

public class Main {
    /**/*from  w  w  w .  java 2s .c  om*/
     * Convert an Image into a TYPE_INT_ARGB BufferedImage. If the image is already of this type, the original image is returned unchanged.
     * @param image the image to convert
     * @return the converted image
     */
    public static BufferedImage convertImageToARGB(Image image) {
        if (image instanceof BufferedImage
                && ((BufferedImage) image).getType() == BufferedImage.TYPE_INT_ARGB)
            return (BufferedImage) image;
        BufferedImage p = new BufferedImage(image.getWidth(null),
                image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = p.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return p;
    }
}

Related Tutorials