ProgLearner
ProgLearner

Reputation: 181

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!

Upvotes: 2

Views: 1791

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168795

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.

Upvotes: 4

Related Questions