Fun with code (since 2006)

ScaleBitmap : 9-slice bitmap class


12.17.07 Posted in Actionscript 3 by

Hi everybody,

I have just finished a simple class which allows you to resize dynamically any Bitmap (just like the MovieClip.scale9Grid feature).

ScaleBitmap

Open Source This project is OPEN SOURCE under RPL License.
2009-02-19 v1.2.2 scaleBitmap_v122.zip

The zip file contains :

  • the v1.2.2 ScaleBitmap class
  • a Flash CS3 demo file
  • a demo PNG Bitmap file

Some features :

  • dynamically set the scale9Grid parameter
  • use setSize() method
  • get the originalBitmapData instance
  • ScaleBitmap extends flash.display.Bitmap

Don't hesitate to post any comments/bugs :)

NEW

Alexandre Legout has contributed to this v1.2 ScaleBitmap which introduce these
new features :

  • bitmapData setter
  • redrawing scale9Grid optimization
  • null scale9Grid capability (restore the default Bitmap class behaviour)

Thanks Lalex :)



80 Responses to “ScaleBitmap : 9-slice bitmap class”

  1. Hey, I really like the Idea!

    Unfortunately I’m having a problem with rotating the scaled Bitmap. It looks distorted like the bitmap is not smoothed. But I pass smoothing = true.

    Do you have an Idea?

    Thank you,
    Raphael ;)

  2. I merged all changes from comments (me, Joel Stransky etc.), cleanded the code and published at http://gist.github.com/232800. I think it will be much easier to have it hosted as a gist…

  3. [...] 15. ScaleBitmap : 9-slice bitmap class The owner have just finished a simple class which allows you to resize dynamically any Bitmap (just like the MovieClip.scale9Grid feature). [...]

     
  4. Jakob E says:

    Very nice class – thanks!

    However I encountered a bug with missing middle section graphics.

    Example(original 80×45 png)

    If using scale9Grid=new Rectangle(10,10,4,10) the
    following bitmap widths causes the middle section not to be drawn:
    600,700,800,900,1000,1500,1600,1700,1800,1900,2000.

    If setting the following bitmap widths it shows just fine:
    100,200,300,400,500,1100,1200,1300,1400,2100,2200

    If I figure out why I’ll post my findings.

    Best,
    Jakob E

  5. chhabeg says:

    thnx… Excellent class

  6. [...] scaling nine Bitmap instances. When developing the MAX Companion, I tried Didier Brun’s ScaleBitmap class, but discovered a costly memory leak in which it instantiates a new BitmapData instance with [...]

     
  7. martin says:

    A Really useful class that’s also super easy to use :P
    What more could I want :) Thanks, man!

  8. [...] 项目地址:http://www.bytearray.org/?p=118 [...]

     
  9. Sides Dissapearing says:

    Hi, Didier.
    Any thoughts on why I’m getting this

    http://bit.ly/ScaleBitmap
    (just move around with the mouse down to test)
    Notice how the sides disappear.
    Here’s a close-up of the bitmap showing the grid.
    Thanks
    http://img717.imageshack.us/i/grid.gif/

  10. [...] Glück gibt es Abhilfe von ByteArray.org. Anwendungsbeispiel: var bg:SomeSwcClip = new SomeSwcClip(); var bgBitmapData:BitmapData = new [...]

     
  11. [...] 15. ScaleBitmap : 9-slice bitmap class The owner have just finished a simple class which allows you to resize dynamically any Bitmap (just like the MovieClip.scale9Grid feature). [...]

     
  12. lolo says:

    very great, thanks !

  13. [...] The ScaleBitmap25 and ScaleBitmap9 classes are based on the ScaleBitmap class created by ByteArray’s Didier Brun. [...]

     
  14. [...] ByteArray’s Didier Brun provided us with a solution to the problem that eliminates most, if not all, the limitations that the native tools and APIs [...]

     
  15. [...] 15. ScaleBitmap : 9-slice bitmap class The owner have just finished a simple class which allows you to resize dynamically any Bitmap (just like the MovieClip.scale9Grid feature). [...]

     
  16. Vemilyus says:

    Hello Didier, I incorporated this really nice class into my open source toolkit.
    Check it out: http://code.google.com/p/as3petoolkit/source/browse/trunk/PET%20Core/src/at/vy/pet/visuals/Scale9Bitmap.as

  17. Greg says:

    If anybody is curious the solution outlined below is over twice as fast to execute:

    http://troygilbert.com/2009/08/quick-fix-for-scale-9-issues/

  18. [...] 原文地址:http://www.bytearray.org/?p=118 [...]

     
  19. michael says:

    Hi!

    I use this class as the base component in a gui framework i wrote for my games.

    In my last project I started to recycle bitmapdata and found a bug when trying to do so. it seems the class is destroying the original bitmapdata when asigning a scale9grid. Reusing the bitmapdata somewhere else causes a #2015 – invalid bitmapdata.

    I am starting investigations.

    michael

  20. michael says:

    Hi!

    I found the reason and it’s not wrong to say:”It’s a feature, not a bug” :-)

    The feature is: As long as scaling isn’t used the class uses the original bitmapdata member of the super class. When asigning a scale9grig, the dispose function of the super class’ bitmapdata is called and the own implementation for bitmapdata is used.

    Clear this saves performance. And there are situations where this behavior is correct and there are other situations where this behavior is wrong.

    I sugest a member to control if the super.bitmapdata is disposed or not.

    My solution looks like this:


    public var disposeSuperBitmapData:Boolean = true;

    private function assignBitmapData( bmp:BitmapData ):void {
    if ( super.bitmapData != null && disposeSuperBitmapData) {
    super.bitmapData.dispose();
    }

    regards,

    michael

  21. michael says:

    Hi!

    I made a test file last night.

    I created an instance of the scaleBitmap class on stage and assigned a scale9grid to it.

    In an enterFrame-Event I created a new Bitmapdata object and assigned it to the scalebitmap instance.

    I completely disabled the call of dispose in the class and watched the memory consumtions of the flash player. It was stable and didn’t increase with each new Bitmapdata.

    As far as I know the flashplayer uses reference counting to determine if objects are still in use or not. If we assign a new bitmapdata object to our instance, flashplayer knows the old one isn’t used anymore.

    But when we want to recycle our bitmapdata object we have a second reference to it from your “bitmapdata library”.

    I think we could remove the dispose call.

    Someof you may argue, that the flashplayer’s behavior isn’t the same in every version/plattform. – You may be right!

    regards,

    michael

  22. vamapaull says:

    I can’t download the zip archive. Is it still available?

  23. Thibault Imbert says:

    Hi vamapaull,

    Yes, just fixed, sorry for the inconvenience.

    Thibault

  24. vamapaull says:

    Thank you for this great and very useful project!

  25. amandus99 says:

    Hi,
    I’m trying to use ScaleBitmap in combination with TweenMax. When I’m resizing the BMP it is growing to the left and down. Is there a possibility to make it individual. So in some cases I want the BMP to grow to the left and up…

Leave a Reply

Open Sort Options

Sort comments by:
  • * Applied after refresh

ByteArray.org
Fun with code since 2006.