i have a bufferedimage and use the getSubimage() method. but it is important for me to create a new instance of the image in the size of the choosed subimage (i want to avoid referencing the whole image).
because bufferedimage has nothing like a copy-constructor
i tried somthing like this:
1 2 3 4 5 6 7 8 9 10 11 12
| ... { BufferedImage frame = createBufferedImage(img.getSubimage(0, 0, 0, 0)); }
public BufferedImage createBufferedImage(BufferedImage image) { ColorModel cm = image.getColorModel(); boolean premultiplied = cm.isAlphaPremultiplied(); WritableRaster raster = image.copyData(image.getRaster()); return new BufferedImage(cm, raster, premultiplied, null); } |
but i get the following exception:
java.awt.image.RasterFormatException: negative or zero width
at java.awt.image.Raster.<init>(Raster.java:1094)
at java.awt.image.WritableRaster.<init>(WritableRaster.java:114)
at sun.awt.image.SunWritableRaster.<init>(SunWritableRaster.java:52)
at sun.awt.image.ByteComponentRaster.<init>(ByteComponentRaster.java:140
)
at sun.awt.image.ByteInterleavedRaster.<init>(ByteInterleavedRaster.java
:177)
at sun.awt.image.ByteInterleavedRaster.createWritableChild(ByteInterleav