简体   繁体   中英

BufferedImage produces black output

BufferedImage for some reason produces black output when I write scaled Image, however Image scales it correctly. I assume here are some problems with painting components. Thank you!

BufferedImage newImage = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);

If putting a PNG or GIF with transparency over it, the transparent parts will become black. It should be:

BufferedImage newImage = new BufferedImage(400, 400, BufferedImage.TYPE_INT_ARGB);

But then, I recommend:

  • Only save as JPEG if the original image was JPEG
  • Not using an ImageIcon to load an Image , instead use ImageIO to load a BufferedImage .
  • Use the buffered image getType() as the parameter instead of BufferedImage.TYPE_..
  • Avoid getScaledInstance(..) like the plague , but if using it, specify Image.SCALE_SMOOTH .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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