How can I get height and width of an image?

前端 未结 5 1044
野的像风
野的像风 2021-01-04 19:40

Why is the following bit of code returns Height: -1 which means that the height is yet not known. How to get height of the image?

 try {
        // Create a          


        
5条回答
  •  失恋的感觉
    2021-01-04 20:13

    Toolkit#createImage(...) is non-blocking. Generally I would rather use javax.imageio.ImageIOto read images.

    To wait for the Toolkit#createImage(...), use:

    MediaTracker mediaTracker = new MediaTracker(component);
    mediaTracker.addImage(image);
    try {
        mediaTracker.waitForAll();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    

    After that, you can call image.getHeight()

提交回复
热议问题