Determine dimensions of an image loaded dynamically

I need to load an image from an external source, (I don't *know* its dimensions). I need to find out the dimensions of the loaded image so that I can position another object relative to it.

I tried this:

_root.MyClip.loadMovie("MyImage.jpg");

The original movieclip's (_root.MyClip) content gets replaced with the image, but the movieclip's dimensions (_width and _height) do not reflect the loaded image's size. They still show the Movieclip's original size.

How do I get the height and width?
ashugarg00Asked:
Who is Participating?
 
negatyveConnect With a Mentor Commented:
You have to wait until the image is completely loaded:

// define a preloader function
function preloader()
{
      // get bytes informations
      var loa = _root.MyClip.getBytesLoaded();
      var tot = _root.MyClip.getBytesTotal();
      // check for loaded complete
      if(loa >= tot && tot > 4){
            // clear the interval (no more checking)
            clearInterval(this.preloaderID);
            // make clip visible
            _root.MyClip._alpha = 100;
            // NOW TRACE RIGHT MEASURES
            trace(_root.MyClip._width);
            trace(_root.MyClip._height);
      }
}

// make MyClip invisible
_root.MyClip._alpha = 0;
// load the image
_root.MyClip.loadMovie("MyImage.jpg");
// start checking
this.preloaderID = setInterval(this, "preloader", 20);
0
 
BillystyxCommented:
once it is loaded do
trace(_root.MyClip._width);
trace(_root.MyClip._height);

Billystyx
0
 
ashugarg00Author Commented:
Thanks negatyve! Just a small question - why do you check " tot > 4" ? Is that the minimum size for a resource?
0
 
negatyveCommented:
why do you check " tot > 4" ? Is that the minimum size for a resource?

No, it's the size of an empty movieclip created at authoring time. When created at run time (via createEmptyMovieClip), its size is 0. Not knowing where your clip came from... :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.