Determine dimensions of an image loaded dynamically

Posted on 2005-04-19
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
    LVL 27

    Accepted Solution

    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;
                // NOW TRACE RIGHT MEASURES

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

    Expert Comment

    once it is loaded do


    Author Comment

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

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
    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…
    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.
    The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now