How to figure out the width and height of an image?

Z

Zsolt

Hi,

how can I figure out the height and width of an image (gif,jpeg, png etc.)
file?

Zsolt
 
A

Andrew Thompson

how can I figure out the height and width of an image (gif,jpeg, png etc.)
file?

Image image = Toolkit.getDefaultToolkit().getImage("pretty.gif");
int width = image.getWidth(null);
int height = image.getHeight(null);

HTH
 
Z

Zsolt

Thank you Andrew,

I got -1 in width and height (Windows-XP, jdk-1.2.4-05). Instead of that I
found an other solution:

ImageIcon image = new ImageIcon(logoTargetFilename);

int width = image.getIconWidth();
int height = image.getIconHeight();

Zsolt
 
A

ak

I got -1 in width and height (Windows-XP, jdk-1.2.4-05).
because image was not loaded.
Instead of that I
found an other solution:

ImageIcon image = new ImageIcon(logoTargetFilename);

int width = image.getIconWidth();
int height = image.getIconHeight();

ImageIcon loads image.

after creating ImageIcon you will also get proper result with
however if you want to get some info about image _before_ it was loaded
e.g. you read image over network, then use Imagero Reader
http://reader.imagero.com.
 
T

Tor Iver Wilhelmsen

Andrew Thompson said:
Image image = Toolkit.getDefaultToolkit().getImage("pretty.gif");

You need to use a MediaTracker here to wait for the image to actually
load before calling
 
A

ak

Image image = Toolkit.getDefaultToolkit().getImage("pretty.gif");
You need to use a MediaTracker here to wait for the image to actually
load before calling
ImageIcon does exactly that thing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top