Toolkit.getDefaultToolkit()。createImage()vs ImageIO.read() [英] Toolkit.getDefaultToolkit().createImage() vs ImageIO.read()
问题描述
我正在使用Swing创建UI,我想在 JLabel
中显示图像。我使用的代码如下:
I'm creating a UI using Swing and I want to display an image in a JLabel
. The code I use is the following:
JLabel label = new JLabel(new ImageIcon(ImageIO.read(new File("img.jpg"))));
如果我使用 png
图像,这个工作正常但是当谈到 jpg
(只有其中一些)时,我得到一个红色图像(与我在Paint.NET中看到的图像不同)。
我使用的图像是这样的: img。 jpg
This works fine if I use png
images but when it comes to jpg
(only some of them), I get a redish image (a different one than the one I see in Paint.NET).
The image I used is this one: img.jpg
所以我尝试(作为替代方案):
So I tried (as an alternative):
Toolkit.getDefaultToolkit().createImage(new File("img.jpg").getAbsolutePath());
- 有没有人知道为什么会这样?是不是支持特殊的
JPEG
格式? - 我在这个论坛上看过大多数人推荐使用ImageIO(例如,此处。为什么?
- Does anyone have an idea of why this happening? Is it a special
JPEG
format which is not supported? - I've read on this forum that most people recommend to use ImageIO (here for example). Why?
非常感谢
推荐答案
如此处所述,您的JPEG图像可能包含虚假透明度信息。一个简单的权宜之计是将图像渲染到具有兼容颜色模型的缓冲区中,如此处所示。
As discussed here, your JPEG image may contain spurious transparency information. One simple expedient is to render the image in a buffer having a compatible color model, as shown here.
这篇关于Toolkit.getDefaultToolkit()。createImage()vs ImageIO.read()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!