I'm trying to save generated graphics as PNG files, but I'm stuck at actually saving the data as a file.
My steps are following:
- Make a graphics objects
- Convert the graphics to a BitmapData via the draw() method
- Encode the BitmapData object to get a ByteArray via the encode method.
- Using Format library (hxformat), save the file
here's my method in Haxe:
function saveImage():Void
{
var ba:ByteArray = image.encode("png");
var bi:haxe.io.BytesInput = new haxe.io.BytesInput(ba);
var data = new format.png.Reader(bi).read();
var out = sys.io.File.write("testRead.png",true);
new format.png.Writer(out).write(data);
}
The image
filed is a class variable type of BitmapData.
Please tell me what I'm doing wrong or how else to save BitmapData as a PNG image.