1

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!

ProgLearner
  • 171
  • 2
  • 15

1 Answers1

3
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.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433