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!
Asked
Active
Viewed 1,580 times
1
-
is it supposed to be transparent? if so .jpg files dont have transparency, you have to use .png – JRowan Nov 25 '14 at 03:21
-
Of course not, this is a normal RGB image, however it does not write to Imagebuffer for some reason. – ProgLearner Nov 25 '14 at 03:23
-
oh, im just thought you meant that its showing up black, transparency in .jpg file would be black – JRowan Nov 25 '14 at 03:25
-
@JRowan thanks for your reply. WHEN BufferedImage.TYPE_INT_RGB produces black AND THEN BufferedImage.TYPE_INT_ARGB, orange color image. – ProgLearner Nov 25 '14 at 03:39
-
2*"BufferedImage.TYPE_INT_ARGB, orange color image."* Are you still saving as JPEG? Saving an `ARGB` image as JPEG will cause unpredictable results - don't do it! – Andrew Thompson Nov 25 '14 at 03:40
-
Did you miss the part where I mentioned ***don't do it***? – Andrew Thompson Nov 25 '14 at 03:43
1 Answers
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 anImage
, instead useImageIO
to load aBufferedImage
. - Use the buffered image
getType()
as the parameter instead ofBufferedImage.TYPE_..
- Avoid
getScaledInstance(..)
like the plague, but if using it, specifyImage.SCALE_SMOOTH
.

Andrew Thompson
- 168,117
- 40
- 217
- 433