title: Save BitmapData/ByteArray as a PNG file
body: <p>I'm trying to save generated graphics as PNG files, but I'm stuck at actually saving the data as a file.</p>
  
  <p>My steps are following:</p>
  
  <ul>
  <li>Make a graphics objects</li>
  <li>Convert the graphics to a BitmapData via the draw() method</li>
  <li>Encode the BitmapData object to get a ByteArray via the encode method.</li>
  <li>Using Format library (hxformat), save the file</li>
  </ul>
  
  <p>here's my method in Haxe:</p>
  
  <pre><code>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);
  
  }
  </code></pre>
  
  <p>The <code>image</code> filed is a class variable type of BitmapData.</p>
  
  <p>Please tell me what I'm doing wrong or how else to save BitmapData as a PNG image.</p>