简体   繁体   English

删除PNG BufferedImage中的透明度

[英]Removing transparency in PNG BufferedImage

I'm reading a PNG image with the following code: 我正在使用以下代码阅读PNG图像:

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

Upon displaying it, there is a black background, which I know is caused from PNG transparency. 在显示它时,有一个黑色背景,我知道这是由PNG透明度引起的。

I found solutions to this problem suggesting the use of BufferedImage.TYPE_INT_RGB , however I am unsure how to apply this to my code above. 我找到了解决这个问题的方法,建议使用BufferedImage.TYPE_INT_RGB ,但是我不确定如何将它应用到我上面的代码中。

Create a second BufferedImage of type TYPE_INT_RGB ... 创建TYPE_INT_RGB类型的第二个BufferedImage ...

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 要保存图像,请查看编写/保存图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM