How can I import external JPGs into Flash using FlashVars?

Dani AI

Generated

As , and pointed out, the common flow is to pass an image URL into the SWF via FlashVars and load it at runtime. If the FLA uses ActionScript 3 (Flash Player 9+), prefer the Loader class for proper COMPLETE/IO_ERROR events and to enable bitmap smoothing; for AS2, use MovieClipLoader to get similar callback control (safer than the simplest built-in calls).

A compact AS3 example that reads a FlashVar named img, loads the JPG, and enables smoothing:

import flash.display.Loader;
import flash.display.Bitmap;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;

var params:Object = loaderInfo.parameters;
var url:String = (params && params.img) ? params.img : "fallback.jpg";

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
loader.load(new URLRequest(url));
addChild(loader);

function onComplete(e:Event):void {
  var content:* = loader.content;
  if (content is Bitmap) Bitmap(content).smoothing = true;
  // resize/position here
}

function onError(e:IOErrorEvent):void {
  trace("Image load failed: " + e.text);
  // fallback UI
}

Pass the variable from HTML with flashvars (for example flashvars="img=images/photo.jpg"). Troubleshooting notes: verify the image URL directly in a browser, URL-encode spaces/special chars, append a timestamp to bust caches while testing, and remember cross-domain rules — if you need to manipulate pixels (BitmapData) from a remote host you will need a crossdomain policy and the proper LoaderContext.

Recommended Answers

All 3 Replies

Member Avatar for Member #114696

Check out

Make a movie clip. The image will be loaded inside thismovie clip, give it an instance name. and try loadmovie.

Your flash var will provide the name/link to the image right:

SO create a movie clip and give it an instance name. Say : mypic

mypic.loadMovie("url from flashvars");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.