Saving image of a movieclip or canvas

I referred this thread [RESOLVED] Saving a png to desktop

But it shows this error :

You cannot access the sys package while targeting js (for sys.io.FileOutput)

Here is the code I am using :

private function loadMain():Void 
{
	addChildViews(); 
	mc = Assets.getMovieClip ("library:MyMovieClip");
	this.addChild(mc);
	
            var bd:BitmapData = new BitmapData(Std.int(mc.width), Std.int(mc.height), true);
	bd.draw(mc);
	saveImage(bd, "test.png");
 
	
}

public function saveImage(image:BitmapData, outputFile:String):Void
{
		var path;
		path = outputFile;
		
		var imageData:ByteArray = image.encode(image.rect ,new  PNGEncoderOptions() );
		var fo:FileOutput = sys.io.File.write(path, true);
		fo.writeBytes(imageData, 0, imageData.length );
		fo.close();
}	

How am I supposed to save image ?

Have you tried FileReference.save()?

It would be good if we integrated this into FileReference for HTML5, in the meantime, here’s code I used on another project:

1 Like

Thanks for sharing. Let me try this code. :slight_smile: