SWF Studio already allows you to save all or part of your application window using ssCore.Win.saveImage but it suffers from one major drawback, you can only save the visible portion of the application window. The technique presented here removes that limitation and allows you to save the contents of any movie clip regardless of whether it's visible or not.

Since the ActionScript 3 Core Library appeared with a JPEG encoder, a number of examples showing how to create JPEG files using a server side script have appeared. You can find a nice example on the Design Reviver blog.

The example presented here is based on the Design Reviver example but with a couple of minor changes that allow you to create JPEG files directly from movie clips in Flash CS3 and Flex projectors created with SWF Studio.

The createJPEG function below requires the ActionScript 3 Core Library from Google Code. You'll need the Northcode ActionScript 3 Utilities library (included with SWF Studio) for the fast ByteArray to hex string conversion function. And finally, make sure you select the FileSys plugin from the SWF Studio Plugins Tab.

import com.adobe.images.JPGEncode;
import northcode.utils.StringUtils;
 
function createJPEG(mc:MovieClip, quality:int, path:String) : int
{
var result:int = 0;
 
var bmp:BitmapData = new BitmapData (mc.width, mc.height);
bmp.draw(mc);
 
var encoder:JPGEncoder = new JPGEncoder(quality);
var bytes:ByteArray = encoder.encode(bmp);
 
var hex:String = StringUtils.byteArrayToHexString(bytes);
var r = ssCore.FileSys.writeToFileBinary({path:path, data:hex});
 
if (!r.success)
result = r.Error.code;
 
return result;
}

The FLA included at the end of this post contains some additional profiling code to help us figure out how long the task of converting a movie clip to a JPEG file on disk takes and, more specifically, how long each part of the process takes.

For testing I used an 800x600 movie clip and told the JPEG encoder to use a quality setting of 90%. At 800x600 the encoding quality setting didn't have a significant effect on the time required to encode the image.

BitmapData created from MovieClip 111
BitmapData has been converted to a JPEG BytaArray 995
ByteArray has been converted to a hex string 13
hex string saved as binary data (JPEG file) 19
total elapsed time 1138

From these results (times above are in milliseconds) you can see that it took just over 1.1 seconds to go from movie clip to JPEG file on disk. Most of that time, almost a full second, was spent in the JPEG encoder.

I thought that saving the raw pixel data and doing the JPEG creation in C++ might be faster but because of the difference in the amount of data to be saved (62 KB for the JPG vs. 1.8 MB of raw pixel data) a quick test showed that it is much cheaper to do the JPEG conversion in ActionScript.

mc2jpeg.zip 310 KB