Reputation: 19534
I'm trying to have a png created from an image in Java display with a transparent instead of white background. Tried setting alpha to 0 in new Color(0,0,0,0) - background appears white still ?
I am using BufferedImage
WritableRaster raster = onscreenImage.getRaster(); WritableRaster newRaster; newRaster = raster.createWritableChild(0, 0, width, height, 0, 0, new int[] {0, 1, 2}); DirectColorModel cm = (DirectColorModel) onscreenImage.getColorModel(); DirectColorModel newCM = new DirectColorModel(cm.getPixelSize(), cm.getRedMask(), cm.getGreenMask(), cm.getBlueMask()); BufferedImage rgbBuffer = new BufferedImage(newCM, newRaster, false, null); try { ImageIO.write(rgbBuffer, suffix, file); } catch (IOException e) { e.printStackTrace(); }
Upvotes: 0
Views: 973
Reputation: 2484
You aren't specifying the alpha component in the color model. Try in this way:
newRaster = raster.createWritableChild(0, 0, width, height, 0, 0, null);
DirectColorModel cm = (DirectColorModel) onscreenImage.getColorModel();
DirectColorModel newCM = new DirectColorModel(cm.getPixelSize(),
cm.getRedMask(),
cm.getGreenMask(),
cm.getBlueMask(),
cm.getAlphaMask());
Upvotes: 0