Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding Image Height And Width using ImageIcon Class

Status
Not open for further replies.

JProg

Programmer
Apr 4, 2002
88
JP
Hi Everyone,

This is kind of a continuation of a previous post. My application is now using ImageIcons to access information about a number of pictures. I am able to successfully "load" the pictures however when I use the methods "getIconHeight()" and "getIconWidth()" to determine the heights and widths of my pictures I simply get -1's returned across the board. The API documentation for ImageIcon isn't great, but from reading the docs on the Image class I believe that my -1's are the result of:

".... (height / width) is not yet known, this method returns -1 and the specified ImageObserver object is notified later."

If anybody out there can help me with determining the real height and width (as opposed to the most unhelful -1) I will be really appreciative. Thanks heaps for your assistance.

Regards

David
 
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class LoadImage
{
private BufferedImage img;
private File imgFile;
private int height;
private int width;

public LoadImage()
{
try{
imgFile = new File("image/a.gif");
img = ImageIO.read(imgFile);
}catch(IllegalArgumentException iae){
System.out.println("IllegalArgumentException in ImageIO.read() method.");
}catch(IOException ioe){
System.out.println("IOException in ImageIO.read() method.");
}catch(NullPointerException npe){
System.out.println("NullPointerException in File() method.");
}

height = img.getHeight();
width = img.getWidth();

System.out.println("The picture is " + height + " pixels high and "
+ width + " pixels wide.");

}

public static void main(String[] args)
{
LoadImage myLoadedImage = new LoadImage();
}
}
 
JProg :

As my reply to your last post, I would use the javax.imageio package.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top