Toolkit.getDefaultToolkit().createImage() vs ImageIO.read()

前端 未结 2 2062
栀梦
栀梦 2021-01-02 13:22

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 Image         


        
相关标签:
2条回答
  • 2021-01-02 13:50

    It looks like you have found a bug in ImageIO.read... (I can reproduce the red tint, and it is definitely not how it should look like).

    You can try to

    • save the JPEG files with other settings
    • open/re-save the file with other programs (hoping to get a more common JPEG-encoding)
    • or use the Toolkit method (if you don't control the images).

    The only problem with the Toolkit method is that the getImage() method returns immediately after it is invoked and the loading is happening on a background thread, so you cannot start working with the Image object immediately.

    0 讨论(0)
  • 2021-01-02 13:53

    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.

    0 讨论(0)
提交回复
热议问题