Take the 2-minute tour ×

How do I set the width and height of a loaded image in Flash? Setting the dimensions immediately after requesting it does not work. The width and height remain zero.

var poster:Loader = new Loader();
poster.load(new URLRequest('http://example.com/image.jpg'));
poster.width = 320;
poster.height = 240;
trace(poster.width); // 0
trace(poster.height); // 0

If I wait a short moment and then set the dimensions, it will work.

I tried listening for the Event.INIT event and Event.COMPLETE events before resizing as suggested by some tutorials. Neither of the events were triggered.

public function theClass() {
    this.poster = new Loader();
    this.poster.contentLoaderInfo.addEventListener(Event.INIT, this.imageLoaded);
    this.poster.contentLoaderInfo.addEventListener(Event.COMPLETE, this.imageLoaded);
    this.poster.load(new URLRequest('http://example.com/image.jpg'));

private function imageLoaded(event:Event):void {
    trace('image is loaded');
    this.poster.width = 320;
    this.poster.height = 240;
share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

In the listener for Event.COMPLETE is the way to do it, so there must be something wrong in your loader code if that event isn't being triggered. I notice you appear to create a local variable "poster" at the top of the code you posted but don't declare it inside of theClass() but I don't know if that's causing your problem here.

Also, customarily you wait until after the image has completed loading before you add it, but again I don't know if that's causing your problem here.

I mean, I just wrote the following code as a minimal test and it works fine:

public function Main():void {
  var loader:Loader = new Loader();
  loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
  loader.load(new URLRequest("image.png"));
private function onComplete(e:Event):void {
  var img:Bitmap = Bitmap(e.target.content);
  img.width = 100;
  img.height = 100;
share|improve this answer
Maybe you should add the part where you set the dimensions in your answer? –  Taurayi Apr 11 '11 at 23:25
I can't believe I didn't think to do that. It's just set e.target.content within the onComplete function, I'll do that right now. –  jhocking Apr 12 '11 at 1:40
I know it seems like I keep nitpicking at your answers, but if I had to suggest one last thing it would perhaps be that inside your event handler you store the e.target object in a local variable. Something like var loader:Loader = Loader(e.target); then store the bitmap from the loader.content object in a local variable like var bitmap:Bitmap = Bitmap(loader.content);, after which you can simply do bitmap.width = 100; bitmap.height = 100;. Don't get me wrong your answer is correct already it's just that storing an event target object in a local variable is a common convention. –  Taurayi Apr 12 '11 at 11:28
I've up voted your answer anyway. –  Taurayi Apr 12 '11 at 11:29
Well thanks for the vote, but I'm not sure how "correct" my answer is. I mean, the code's correct, but I suspect Adam's answer below may actually be more helpful to jojo. –  jhocking Apr 12 '11 at 12:07
show 3 more comments

Is the image on a different domain than your swf? If so, and your Event.COMPLETE listener isn't working, it could be due to a security exception. The server with the file must have a crossdomain.xml policy file that allows access to the domain your swf is hosted on, otherwise you won't be able to manipulate the size of the loaded image directly as shown in jhocking's example.

If you know there is a policy file on the server hosting the image (by going to http://site.com/crossdomain.xml and seeing that it allows your domain or a wildcard that covers your domain), then create a loaderContext for your loader to use that sets the checkPolicyFile flag to true:

var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;
this.poster.load(new URLRequest('http://site.com/image.jpg'),context);

In your onComplete handler, check Loader.childAllowsParent to see if you have access to the loader content. If so, set the loader.content.width and loader.content.height.

share|improve this answer
add comment

It's generally a good idea to add an IOErrorEvent listener to all loads. If there's a problem then this will tell you:

this.poster.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);

function errorHandler(e:IOErrorEvent):void {
trace("Error on load: " + e);

If there is a problem in the load process, this is how you catch it.

share|improve this answer
add comment

Your Answer


