Determine dimensions of an image loaded dynamically

Posted on 2005-04-19
Medium Priority
Last Modified: 2010-04-03
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:


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?
Question by:ashugarg00
  • 2
LVL 27

Accepted Solution

negatyve earned 1000 total points
ID: 13814819
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)
            // make clip visible
            _root.MyClip._alpha = 100;

// make MyClip invisible
_root.MyClip._alpha = 0;
// load the image
// start checking
this.preloaderID = setInterval(this, "preloader", 20);
LVL 18

Expert Comment

ID: 13814836
once it is loaded do


Author Comment

ID: 13815323
Thanks negatyve! Just a small question - why do you check " tot > 4" ? Is that the minimum size for a resource?
LVL 27

Expert Comment

ID: 13823270
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... :)

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question