How to save a Image to PNG format?

6.2k views Asked by At

Now Im saving to text format, and I got a error: TypeError: Error #1009: Cannot access a property or method of a null object reference. at SaveImage/onClick()[/Users/VVT/Documents/Adobe Flash Builder 4.6/SuperDraw/src/SaveImage.as:40] I wanna change my code so I can save to PNG format?

public class SaveImage extends Sprite

{
    private var btnSave:buttonSave;
    //private var ba:ByteArray;
    private var file:FileReference;

    public function SaveImage()
    {
        // Skapar min knapp.
        var btnSave:buttonSave = new buttonSave();
        addChild(btnSave);
        btnSave.x = 400;
        btnSave.y = 440;

        btnSave.addEventListener(MouseEvent.CLICK, onClick);

        var file:FileReference = new FileReference();           
    }

    private function onClick(evt:MouseEvent):void 
    {
        //var ba:ByteArray = file.encode(bitmapData);
        //file.save(file);
        file.save("some text. \nsome more text", "actionsnippet.txt");
    }
}
1

There are 1 answers

3
kapex On BEST ANSWER

You have a property named file, yet you are creating and initializing a local variable with the same name in this line of your constructor:

var file:FileReference = new FileReference(); 

Don't worry, those mistakes happen. Remove the var and type to get rid of that null reference error.

file = new FileReference();

To save the image as png, the as3corelib library, which is mentioned in this answer of the question linked in this comment, looks quite promising. Import the library and let it encode your bitmapdata:

file.save(PNGEncoder.encode(bitmapData));