Flash 10 BitmapData.draw fails beyond 4096 pixels?

So Flash 10 allows us to create bitmaps that are larger than the 2880 pixels. Up to 8192 wide or tall (though not both – there’s a maximum pixel count). I’ve covered this in depth in a previous post.

But it seems like there is a bug with the draw method. I can’t seem to draw anything in it beyond 4096 pixels. Here’s an example:

[as][SWF(width=800, height=200, backgroundColor=0xffffff, frameRate=31)]

var bmpd:BitmapData = new BitmapData(8000, 2000, false, 0x00ffff);
var bmp:Bitmap = addChild(new Bitmap(bmpd)) as Bitmap;
bmp.width = 800;
bmp.height = 200;

var s:Sprite = new Sprite();
s.graphics.beginFill(0xff0000);
s.graphics.drawRect(0, 0, 90, 90);
s.graphics.endFill();

for(var i:int = 0; i < 8000; i+= 100) { bmpd.draw(s, new Matrix(1, 0, 0, 1, i, 100)); }[/as] What's happening here is I'm making a BitmapData that is 8000x2000, adding it to a Bitmap and putting that Bitmap on stage. Then sizing the Bitmap itself down so you can see the whole thing on stage (800x200). Then I'm creating a sprite and drawing a red rectangle in it. And finally, drawing that sprite to the BitmapData repeatedly from 0 to 8000 on the x axis. But, as you can see here, what happens is that the drawing fails about half way across - 4096 pixels: bitmap_error

You can see a similar problem by drawing a 8000 pixel wide rectangle in the sprite and drawing it once to the bitmap. Only the first 4096 pixels will appear on the bitmap.

I’ve searched around and don’t see anyone talking about this. Is this a known error? Any workarounds?

This entry was posted in ActionScript, Flash. Bookmark the permalink.

21 Responses to Flash 10 BitmapData.draw fails beyond 4096 pixels?

  1. subb says:

    Well as a workaround, you can always make 2 (or more) smaller bitmaps and create a class that handle the drawing to those bitmaps.

  2. Bob says:

    I’ve had this problem too, but not only with bitmaps, it seems to have just stopped drawing in general after 4096 pixels, like it’s the end of the stage 😉

    It was before fp 10 though, fp9 something…

  3. Erik Natzke says:

    I’ve run into this before as well. I believe the restraint is a combined pixel size (ie 8000×1 would work fine, but 8000×2000 will go beyond that threshold). As a result I have had to create a system of tiles in order to output large images.

    Performance also seems to increase if I don’t push the size of the tiles beyond 2000×2000, so I end up with 5×5 grid of bitmaps if I’m wanting to output something to 10,000 by 10,000. That means that each time I would normally draw something to one bitmap, I end up drawing it to the entire array of bitmaps.

    Then you output the tiles via pngencoder*

    *Since I hate having to stitch all those tiles together each time I create an image, I’ve even added some extra code to piece them all together (but if you aren’t doing this all that much it’s maybe not worth having to code it).

  4. kp says:

    well, it’s not a problem with creating the bitmap. as i covered, here: http://www.bit-101.com/blog/?p=1426 I know the limits of that.

    the bitmap I can draw to. just can’t draw to any coordinate beyond 4096 in it.

  5. Erik Natzke says:

    This I understand, which is why I said you need to resort to an array of bitmaps to create larger images.

    It is interesting that you can create a bitmap at 8191, but you can’t draw beyond half 4096. I would imagine it’s a memory limitation place on the player to prevent bad memory crashes.

  6. kp says:

    Yeah, I got what you are saying now. In essence, we can make big bitmaps, as long as we don’t actually try to do anything with them. 🙂

  7. Erik Natzke says:

    Well. what is it that you are trying to do with them?

    Yes it’s harder to perform filtering operations on them (ie doing a blur will begin to show the borders of the bitmaps), but there are ways around that too.

  8. Hey! I ran in to all this problems and as Erik mentioned above I’ve end with multi-bitmaps solution. I also have to rebuild JPEG encoder class to encode multi-bitmaps in to single JPEG file. You can check it out in my Mosaic application:
    http://www.inspirit.ru/exchange/mosaic/

    I’ve also posted about this method:
    http://blog.inspirit.ru/?p=201

    I’m sure the the fastest way is to create tile based Bitmap class as Keith did before and to use it for large bitmap generations.

  9. Cay says:

    I’ve had the same problem and I think that it’s not really a draw() limitation, but a DisplayObject one… for instance:
    var bmp=new BitmapData(8000,100,false,0);
    var bmp2=new BitmapData(8000,10,false,0xFF0000);
    bmp.draw(bmp2);
    works perfectly… same with other methods like fillRect and applyFilter.
    However, it fails if you try to draw() any DisplayObject beyond 4079 pixels in horizontal, or 4082 in vertical (according to getColorBoundsRect).

  10. makc says:

    Re: “I’ve searched around and don’t see anyone talking about this”

    because, frankly, only very few people in the world really needed it, ever.

    Check http://code.google.com/p/bitmapdataunlimited/ maybe they have a workaround

  11. Martin says:

    hey guys, I posted about that issue a few month back. http://blog.formatlos.de/2008/12/11/bitmapdatadraw-is-limited-to-4096px/ and also put a small workaround in the BitmapDataUnlimited class (http://blog.formatlos.de/2008/05/28/bitmapdataunlimited/). cheers martin

  12. Zevan says:

    Looks like its a problem with draw(), because your example works if you replace draw with copyPixels()

  13. personman says:

    Hey, your first Flash-related post in months!

  14. Mr.doob says:

    Cay has leading. As usual. Thanks!

  15. Mark Knol says:

    In my opinion the bitmapdata restriction just kinda sucks. Ok; I get the idea of memory issues, especially at online purposes. But why can’t we use really(!) large bitmaps local or with AIR?

    To go beyond the limitation; I don’t use bitmaps larger than 4000×4000, but use the same idea as Erik (a tile system class). All tiles are exported as single PNG files, and merged using my Tiler app (http://tiler.stroep.nl).

    The Bitmapdata object has (always has) a strange behaviour when you go pass the limits. No errors too.. Irritating..

  16. Joshua says:

    Have you ever tried to embed a FLA that is larger than 4096 pixels in height? I recently developed an application that resizes based upon the amount of content you have. Well, once you pass the 4000 mark the movie sort of explodes. Some of the graphics, appear, others don’t, and the behaviors are dodgy.

    I think that this 4000 pixel limit goes farther than simply the draw() method. I wonder how the same code would behave in an AIR application? Assuredly this is directly related to Flash being designed for the browser, but it obviously has its drawbacks when it is being used for a large touch screen, or if you get a little “too creative”.

    I had to write Javascript to scroll my Flash content then command my Flash application to scroll itself in order to make the application feel any longer than this limit. Practically speaking, I’m sure that memory use begins to get exponentially worse as you go past 16,000,000 pixels.

  17. Simon says:

    K this is going to be an easy one for someone to answer.

    How do I set up Flex Builder (actually using the Eclipse plugin) to take advantage of these big bitmapDatas? I’ve got a new flex sdk added (3.3.0), and I’ve run the Air project with app-descripter set to version 1.5, but I can’t seem to declare: new BitmapData(4000,4000); It just throws the Invalid bitmapdata” error.

  18. Simon says:

    Nobody wanna answer? It’s probably easy.

  19. Adrian says:

    @Simon : make sure that they either implement the IUIComponent interface or inherit from the UIComponent. I would use the interface option considering that these classes need to implement a lot of their own parent code (IBitmapDrawable) in order to get the desired results. I think… I could be WAY off…

  20. Markus says:

    @Simon: Well by now you hopefully found the solution, but for others stumbling upon: If you use sdk level < 4, you have to build the app for flash player 10 specifically, as described in the help.

Leave a Reply