9-Slice and Bitmap

Have you ever tried to take a snapshot using bitmap.draw() of a movieclip that has 9-slice enabled?

//my9SliceEnabledMc is a 40 by 40 9-slice enabled movieclip on stage
my9SliceEnabledMc.width = 200;
my9SliceEnabledMc.height = 300;
var myBitmapData = new BitmapData(200,300);
myBitmapData.draw(my9SliceEnabledMc);
addChild(new Bitmap(myBitmapData));

So what you'd expect to happen above is to have a new bitmap that looks the same as my9SliceEnabledMc,
but what you get instead is the original 40x40 movieclip.
This doesn't make sense at all, i have no idea what Adobe was thinking when they let this one through.
This was driving me crazy for a while now, and today i finally found the answer, as simple as it seems,
putting the my9SliceEnabledMc inside another sprite corrects the issue!!

//my9SliceEnabledMc is a 40 by 40 9-slice enabled movieclip on the library
var myContainer = new Sprite();
var my9SliceEnabledMc = new my9SliceEnabledMc();
myContainer.addChild(my9SliceEnabledMc);
my9SliceEnabledMc.width = 200;
my9SliceEnabledMc.height = 300;
var myBitmapData = new BitmapData(200,300);
myBitmapData.draw(myContainer);
addChild(new Bitmap(myBitmapData))

Also i should add that this is a major issue with PV3D since you cannot directly use a 9-slice item as a texture.

2 Responses to “9-Slice and Bitmap”

  1. Tiemen Glastra Says:

    When using BitmapData.draw, the untransformed version of the source material is used. The quick & dirty way to fix this is indeed put the child inside an untransformed Sprite or MovieClip instance, in which case the child is rendered without transformation coordinates.

    The target however, is only the first property assignable to the draw() call. The second and most interesting is the matrix to use for transformations. Any object that has been moved, scaled or rotated has a non-default transformation matrix. Using this as a source will allow you to capture the target including all transformations.

  2. Idoru Says:
    Hey Tiemen,
    Right you are, but the problem is that papervision doesn’t support passing the matrix in the material constructor.
    But thx, i never actually thought of that :)

Leave a Reply