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 URL for the image's location
URL url = new URL("http://bmw-2006.auto-one.co.uk/wp-content/uploads/bmw-m3-2006-3.jpg");
// Get the image
java.awt.Image image = Toolkit.getDefaultToolkit().createImage(url);
System.out.println("Height: " + image.getHeight(null));
} catch (MalformedURLException e) {
} catch (IOException e) {
}
Use
ImageIO.read(URL)orImageIO.read(File)instead. It will block while loading, and the image width & height will be known after it returns.E.G.
Alternately, add a
MediaTrackerto the image being loaded asynchronously by theToolkitand wait until it is completely loaded.