How to directly save a BitmapData as a PNG file without any dialog

First time asking a question here so sorry if I do something wrong.

I’m using Haxeflixel (compiling to c++) and I’ve been trying to figure out how I can encode and save a BitmapData as a PNG to a file without any kind of dialog like the sys.FileSystem.saveContent and sys.FileSystem.saveBytes functions, but every method I try either isn’t valid and won’t compile or creates an invalid PNG that can’t be read as an image at all.

Currently this is basically what I have:

function saveBitmap(image:BitmapData)
    var bytes:ByteArray = PNGEncoder.encode(image);
    sys.FileSystem.saveBytes("assets/images/Encoded Image.png", bytes);

This code compiles and runs just fine but the PNG image created isn’t valid

Can anyone explain why this doesn’t work and how I make it actually work as I’ve been trying to figure this out for far too long.


  • i've managed to make it work using

    var bytes:ByteArray = image.encode(image.rect, new PNGEncoderOptions);

    instead of

    var bytes:ByteArray = PNGEncoder.encode(image);

    i guess either PNGEncoder doesnt work or i was using it wrong

    edit: the images created can be read by everything i've tried exept Haxeflxiel itself for some reason, i've tried different BitmapData's made from different methods but it's still the same, could use help with that too.