Create a new buffered image with the same characteristics (color model, raster type, properties...) than the specified one. - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Create

Description

Create a new buffered image with the same characteristics (color model, raster type, properties...) than the specified one.

Demo Code


//package com.java2s;
import java.awt.image.BufferedImage;
import java.util.Hashtable;

public class Main {
    /**/*from   ww  w  .j  ava2  s .c  o m*/
     * Create a new buffered image with the same characteristics (color model,
     * raster type, properties...) than the specified one.
     *
     * @param width the width
     * @param height the height
     * @param image an image with the same characteristics than the one which
     * will be created.
     * @return
     */
    public static BufferedImage createBufferedImage(int width, int height,
            BufferedImage image) {
        Hashtable<String, Object> properties = null;
        String[] propertyNames = image.getPropertyNames();
        if (propertyNames != null) {
            properties = new Hashtable<>(propertyNames.length);
            for (String propertyName : propertyNames) {
                properties.put(propertyName,
                        image.getProperty(propertyName));
            }
        }
        return new BufferedImage(image.getColorModel(), image.getRaster()
                .createCompatibleWritableRaster(width, height),
                image.isAlphaPremultiplied(), properties);
    }
}

Related Tutorials