Bitmapdata.encode method [Solved]

The params don’t seem to match up with the actual function. I need to encode a png but what do I use for the compressor parameter ? Any alternative will also help :wink:.

Taken from the github

 * @param format  The encoding format, either "png" or "jpg".
 * @param quality The encoding quality, when encoding with the JPG format.
 * @return  A ByteArray in the specified encoding format
public function encode (rect:Rectangle, compressor:Dynamic, byteArray:ByteArray = null):ByteArray 
    	openfl.Lib.notImplemented ("BitmapData.encode");
    	return null;


I just added it. Now you should be able to (from the source repository or in the next release) use this as expected. For now, the rectangle argument is not supported, but for a full image this should work :smile:

var bitmapData = Assets.getBitmapData ("assets/openfl.png");
var png = bitmapData.encode (bitmapData.rect, new PNGEncoderOptions ());
trace (png.length);
var jpeg = bitmapData.encode (bitmapData.rect, new JPEGEncoderOptions (90));
trace (jpeg.length);

The “fast” PNG encoder option is also ignored at the moment (I’d like to think it’s always fast ;)) but the “quality” JPEG option should be supported properly on all targets. In Flash Player, it should use the built-in version added in Flash Player 11.3, and on HTML5 I believe encoding may require that you use <haxelib name="format" /> and might be slow

I’m getting error on html5 (Cannot read property ‘length’ of null). Adding format doesn’t fix this. Is there other way to encode bitmapdata on html5?

edit: it seems that this was added only in “v2” package which is not available in html as far as I know :confused:

Hmm, maybe there’s just a little bug. I believe that this should work (in principal) in HTML5, and no, it is in OpenFL 3 without -Dlegacy, just try a desktop build and does it work?

This works fine on desktop (newest openfl/lime, no -Dlegacy)

var bdm:BitmapData = new BitmapData(200, 100);
trace(bdm.encode(bdm.rect, new PNGEncoderOptions ()));

encode function in BitmapData does something like that

return byteArray = __image.encode ("png");

and encode function in lime’s image does that:

#if (!html5 && !flash)
stuff that actually encodes :)
return null;

So encoding always returns null on html…or maybe I’m just doing something wrong :slight_smile: Anyway I have found a way to “extract” HTMLImageElement from bdm which can be converted to base64 and this suits me fine but I’d prefer to use encode function.

Oh, have you tried adding <haxelib name="format" />?

Yeah, but it doesn’t change anything.

Hey everyone,

i am looking for the solution for this problem. I am using HTML5, and I get null from the bitmapdata.encode() method. I tried using both “png” and new PNGEncoderOptions(), but the result was the same.

What version of OpenFL and Lime are you using? I believe this was just recently added, without needing format, either :smile:

Yeah I was using earlier version. But I still get null ByteArray as a result, even after changing the changes in this 3 files.

actuate: [1.8.6]
box2d: [1.2.3]
flixel-addons: [1.1.0]
flixel-tools: git [dev:C:\HaxeToolkit/flixel-tools/git]
flixel-ui: [1.0.2]
flixel: 3.3.11 git [dev:C:\HaxeToolkit/flixel/git]
haxelib_client: [3.2.0-rc.3]
hxcpp: [3.2.102]
layout: [1.2.1]
lime-samples: [2.6.0]
lime-tools: [1.5.7]
lime: 1.0.1 [2.6.2]
nape: [2.0.19]
nme: [5.4.2]
openfl-bitfive: 2.0.1 [3.3.3]
openfl-html5: [1.0.5]
openfl-native: [1.4.0]
openfl-samples: [3.3.0]
openfl: 2.0.1 [3.3.3]
swf: [2.0.2]

Could you try the latest?

I tried the latest, but

var bitmap:Bitmap = FlxScreenGrab.grab(new Rectangle(0, 0, 798, 438), false, true);
var byteArray:ByteArray = bitmap.bitmapData.encode(new Rectangle(0, 0, 798, 438), PNGEncoderOptions);

Resulting byteArray is null.

Try using new PNGEncoderOptions () instead of passing the class name :slight_smile:

1 Like

I cannot confirm that this is working on the HTML5 target unfortunately. Some environment info:

  • OpenFL: 3.5.3-LQCE4x
  • Lime:2.8.3

I was using a Flixel-based workaround, and switched to pure OpenFL this morning. I can confirm that the BitmapData is properly converting to Bitmap (it can be rendered on the screen.

Approach 1
bitmapData.encode( bitmapData.rect, 'png' )

This returns null.

Approach 2
bitmapData.encode( bitmapData.rect, new PNGEncoderOptions () )

This throws an error "bytes is null" at

  1. openfl.utils.ByteArray.fromBytes
  2. BitmapData.encode

We may want to update these docs when/if this is resolved:

Issues with the docs:

  1. It does not list the image property which is a public property on BitmapData
  2. It says the encode function is not avaialble on HTML5 targets, but I think your above message says that it should be.

function encode(rect:Rectangle, compressor:Object, byteArray:ByteArray):ByteArray
Encodes the current image as a JPG or PNG format ByteArray.

This method is not available to the HTML5 and Flash targets.

@param format The encoding format, either “png” or “jpg”. @param quality The encoding quality, when encoding with the JPG format. @return A ByteArray in the specified encoding format

@CharlesAnifowose Hi, it’s available and it’s working on html5 target.

On lime 2.9.1 it doesn’t work.

Got a pull request with the fix ready:

There was a misplaced #end macro tag that prevent the html5 conversion from working.