woninana
woninana

Reputation: 3481

how to save an image from flash using actionscript 2.0?

I'm trying to make a character creator application using flash actionscript 2. I'm quite done with the character's moveable parts, however my main concern is on how to save the image as jpeg/png. I tried to search for tutorials on the net but most of it would suggest using php.

Is there any way that i can save an image without the use of php? I would like to save my characters image using a button, and save it directly to my pc.

Upvotes: 0

Views: 2663

Answers (2)

Subash Selvaraj
Subash Selvaraj

Reputation: 3385

You can create a container in AS3 and load your AS2 application into it. So you can use AS3 code in that container to save the image into your PC. Let me know if you need help regarding this.

You need to deploy both the files in your release. AS3 swf will be your primary swf and AS2 swf will be loaded into it.

// code

package

{

import asfiles.encoding.*;

import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.net.*;
import flash.utils.ByteArray;

public class AS3Container extends MovieClip
{

    public var myRequest:URLRequest;
    public var l:Loader;

    public function AS3Container()
    {   
       myRequest = new URLRequest("scene.swf"); // load your AS2 swf
       l = new Loader();
       l.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
       l.load(myRequest);

       saveButton.enabled = false;         

       saveButton.addEventListener(MouseEvent.CLICK, saveHandler);
    }// end function

    public function onComplete(event:Event)
    {
       this.addChild(l);
       l.width = stage.width;
       l.height = stage.height;
       saveButton.enabled = true;

    }// end function

    public function saveHandler(event:MouseEvent) : void
    {                     
        saveImage(l,"myImg.jpg");   

    }// end function

     public function saveImage(image:Loader, imageName:String) {

            var _rect:Rectangle = null;
            var _matrix:Matrix = null;
            trace(image.x+","+image.y);             
            _rect = new Rectangle(image.width/2, image.height/2, image.width, image.height);                
            var jpgSource:BitmapData = new BitmapData(image.width, image.height);
            jpgSource.draw(image, _matrix);
            jpgEncoder = new JPEGEncoder(100);
            var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);               
            var file:FileReference = new FileReference();
            file.save( jpgStream, imageName );

    }

}

}

The approach used in this link will help you to save using AS3 and PHP. http://subashflash.blogspot.in/2013/10/save-image-from-as2-project-using-as3.html.

Upvotes: 1

user797257
user797257

Reputation:

Not possible using AS2. In order to save an image you would need to write it to a format that allows null bytes (i.e. octets of bits which are all unset). Your best approach to a byte sequence is a string, but null-byte would terminate it.

Upvotes: 0

Related Questions