Removing transparency in PNG BufferedImage

前端 未结 1 2030
臣服心动
臣服心动 2020-12-07 03:08

I\'m reading a PNG image with the following code:

BufferedImage img = ImageIO.read(new URL(url));

Upon displaying it, there is a black back

相关标签:
1条回答
  • 2020-12-07 03:42

    Create a second BufferedImage of type TYPE_INT_RGB...

    BufferedImage copy = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
    

    Paint the original to the copy...

    Graphics2D g2d = copy.createGraphics();
    g2d.setColor(Color.WHITE); // Or what ever fill color you want...
    g2d.fillRect(0, 0, copy.getWidth(), copy.getHeight());
    g2d.drawImage(img, 0, 0, null);
    g2d.dispose();
    

    You now have a non transparent version of the image...

    To save the image, take a look at Writing/Saving an Image

    0 讨论(0)
提交回复
热议问题