clone Buffered Image - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage

Description

clone Buffered Image

Demo Code


//package com.java2s;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.util.Hashtable;

public class Main {
    public static BufferedImage cloneBufferedImage(BufferedImage image) {
        String[] pnames = image.getPropertyNames();
        Hashtable<String, Object> cproperties = new Hashtable<String, Object>();
        if (pnames != null) {
            for (int i = 0; i < pnames.length; i++) {
                cproperties.put(pnames[i], image.getProperty(pnames[i]));
            }//from   ww  w  .j ava  2 s  .  co m
        }
        WritableRaster wr = image.getRaster();
        WritableRaster cwr = wr.createCompatibleWritableRaster();
        cwr.setRect(wr);
        BufferedImage cimage = new BufferedImage(image.getColorModel(), // should be immutable
                cwr, image.isAlphaPremultiplied(), cproperties);
        return cimage;
    }
}

Related Tutorials