我不知道它是否适用于@Jos éRobertoAraújoJúnior 讨论的 Image Type 0。 但是: 图像类型0是无效的,不应该发生。
/**
* Copied from
* <a href=https://github.com/jfree/jfreechart/blob/master/src/main/java/org/jfree/chart/util/PaintAlpha.java>JFreeChart PaintAlpha</a>
*
* @param srcImage
* @return
*/
public static BufferedImage cloneImage(final BufferedImage srcImage) {
final WritableRaster srcRaster = srcImage.getRaster();
final WritableRaster dstRaster = srcRaster.createCompatibleWritableRaster();
/*
* This is the code that actually COPIES the pixels...
*/
dstRaster.setRect(srcRaster);
/*
* Images hardly ever have Properties, but we copy them anyway...
*/
final String[] propNames = srcImage.getPropertyNames();
final Hashtable<String, Object> props;
if (propNames == null) {
props = null;
} else {
props = new Hashtable<>();
for (int i = 0; i < propNames.length; i++) {
props.put(propNames[i], srcImage.getProperty(propNames[i]));
}
}
/*
* That's it folks! Return the new clone...
*/
return new BufferedImage(srcImage.getColorModel(), dstRaster, srcImage.isAlphaPremultiplied(), props);
}