Detect image load then get its dimensions

I am trying to load an image from an external source and then get the _x _y _height and _width of the movieclip that I have loaded the image into.

I have successfully loaded the image into a movie clip but because of the way Flash works I can not pull up the attributes of the movieclip.

I basically need Flash to detect when the actual image has been loaded into flash so that then I can get its attributes.

Any help would be wonderful.

Hi
Are you using MX ? If so it should be pretty straightforward depending on how you are loading in the image. Can you post the code you are using

SteveD

Yeah i’m using MX

Here is the code …

image.loadMovie("./images/" + this.picture);
image.onLoad = function(success){
if(success){
image._y = dynamictext._height+50;
dynamictext2._y = 30 + image._y + image._height;
} else {
trace(“not loaded”);
}
};

It loads the image but the image.onLoad function does not work.

Hi,
Not sure what you are trying to achieve with your onLoad function
Are dynamictext & dynamictext2 text boxes ?
Try tracing the size of the image like this

[AS]
image.onLoad = function(success)
{
if(success)
{
trace(image._width && image._height)
}
else
{
trace(notLoaded)
}
}[/AS]

If that doesn’t give you thre required result change image to this.picture

Also when posting code it is easier for people to read if you can use the tags at the top of the thread

let me know how that works

SteveD

Sorry i didn’t realize that I could do that with the actionscript. What you suggested didn’t work. I’ll explain what I am doing…

[AS]
//I am loading a picture where “picture”
// is the name of the picture (mypic.jpg)
image.loadMovie("./images/" + this.picture);
//I want to make sure that the picture is
// loaded before i run my commands because of how
//Flash works that unless i make sure that the imgae
//is fully loaded it will continue to get its _height
//even if it isn’t loaded, resulting in a 0
image.onLoad = function(success){
if(success){
//I want to set the y pos of movieclip image
// (containing loaded picture). The image pos
// will be set below the textbox “dynamictext” according
//to its height.
image._y = dynamictext._height+50;
//“dynamictext2” (another textbox) will be placed
//underneath the image. So the order
// is “dynamictext”, “image”, "dynamictext2"
dynamictext2._y = 30 + image._y + image._height;
} else {
trace(“not loaded”);
}
};[/AS]

The main problem is that the onLoad function does not work. It can’t tell if the image is loaded or not. My picture loads but the function doesn’t run, it doesn’t even say “not loaded” and I substituted in what you suggested and it doesn’t even trace its height.

Hi,
yeah, I agree, what I suggested doesn’t work - just been testing it myself. After looking at the Flash help files (F1) the succes function needs to be in an onEnterFrame

This will trace success

[AS]
image.onEnterFrame = function(success)
{
if(success)
{
trace(success);
}
}[/AS]

Now it just a question of getting your code to work when success = true !!

Will give it a try and get back - perhaps you will post again if you get it working first

SteveD

What’s REALLY annoying is that if I press a button to advance to the next frame, with the image movieclip still there it can then find the attributes of the movieclip. It is driving me absolutely nuts :m:

Hi,
Agree - it is starting to make me a little crazy as well. I have an idea that using the succes function with images doe not work the same way as when loading in text. Looking at the Flash help files I think you need to use getBytesLoaded and totalBytesLoaded a bit like loading in an swf.

I have seen a tute somewhere on loading jpg’s in this way, but cannot for the life of me remember where.
have you done a search on these forums for dynamically loading jpg’s ? Just an idea - i will keep looking for you tho’ i am getting short on time for today.

It might be an idea to repost this Q and hope that someone like senocular or lostinbeta jump in as I feel sure they will know the answer.

Sorry I couldn’t help more, will post if I find a solution

SteveD

thanks for checking. I’ll definitely look at the bytesloaded idea and see if that works.

I did repost the thread, especially after I read the READ THIS BEFORE POSTING thread, and realized what a retard I am. Hence my username. block:

Hi bozo,
Didn’t jump into your new thread, neither has anyone else I notice !
Looked around a bit at actionscript.org for you and came up with a couple of threads that may be of use to you. You will definitely need to use getBytesLoaded etc etc before performing any actions on your jpg’s, try these, let us know how you get on

resize dynamically loaded jpg’s
http://www.actionscript.org/forums/showthread.php3?s=&threadid=20371&highlight=dynamically+load+jpgs

preloader for jpgshttp://www.actionscript.org/forums/showthread.php3?s=&threadid=22423&highlight=dynamically+load+jpgs

** and you can find the entire thread here if the above 2 are no good**
http://www.actionscript.org/forums/search.php3?s=&action=showresults&searchid=1094619&sortby=lastpost&sortorder=descending

Hope that helps

SteveD

=) Thanks, for those links, much appreciated. I posted what I had found in the other thread. The code is rather large and cumbersome, but hopefully your links will help me to narrow the code down a bit more.