• Post Reply Bookmark Topic Watch Topic
  • New Topic

image width and height  RSS feed

 
pavan sharma
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using this statement in jsp page to get image and it's width and height -

Image im = Toolkit.getDefaultToolkit().getImage("E:/photos/thumb/first.jpg");


width = im.getWidth(null);
height = im.getHeight(null);


here problem is that , first time it returns -1 to width and height and after refershing page it returns actual width and height of image.


I think this problem is related to awt and image classes so i posted to it in awt section rather than jsp section.

just tell me how can i get actual width and height in first time.
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like the image hasn't completely loaded the first time you call getWidth and getHeight.

Use a MediaTracker to obtain the image instead.

Cheers,
James
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
width/height works OK if you can create your image like this

Image im = new ImageIcon("Test.gif").getImage();
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!