In a previous post (Finding a pixel’s color value using the Bitmap classes and getPixel()) we looked at copying an image so we could build a simple color-picker like app. In this post, we explore duplicating a loaded image and copying it into a TileList control. Each time you press the “Copy image” button, a new instance of the source image is created and added to the TileList control’s data provider.

As an added bonus, we also create a custom item renderer consisting of an HBox container, Image control, and a Label control.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/03/duplicating-images-using-the-bitmap-and-bitmapdata-classes/ -->
<mx:Application name="Image_content_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">
 
    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
 
            [Bindable]
            private var arrColl:ArrayCollection = new ArrayCollection();
 
            private function dupeImage(source:Image):void {
                var data:BitmapData = Bitmap(source.content).bitmapData;
                var bitmap:Bitmap = new Bitmap(data);
 
                arrColl.addItem({image:bitmap, label:"item #" + (arrColl.length + 1)});
            }
        ]]>
    </mx:Script>
 
    <mx:HBox>
        <mx:Panel title="Source image">
            <mx:HBox verticalAlign="middle" horizontalAlign="center" width="100%" height="100%">
                <mx:Image id="img1" source="assets/logo.png" />
            </mx:HBox>
 
            <mx:ControlBar>
                <mx:Button label="Copy image" click="dupeImage(img1)" />
            </mx:ControlBar>
        </mx:Panel>
 
        <mx:TileList id="tileList" dataProvider="{arrColl}" width="300" height="200" columnCount="4" verticalScrollPolicy="on">
            <mx:itemRenderer>
                <mx:Component>
                    <mx:VBox>
                        <mx:Image source="{data.image}" />
                        <mx:Label text="{data.label}" />
                    </mx:VBox>
                </mx:Component>
            </mx:itemRenderer>
        </mx:TileList>
    </mx:HBox>
 
</mx:Application>

View source is enabled in the following example.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

28 Responses to Duplicating images using the Bitmap and BitmapData classes

  1. li wenzhi says:

    Good sample of bitmap operation, thank your for sharing experience!

  2. Anonymous says:

    Very nice! I wait for the next example.

  3. Good one! Thanks.

    ~Tushar

  4. techpop says:

    This is a great example. How about taking it a step further. Is there a way to grab “stills” from a playing flv, let’s say it’s a basic photo slideshow and compare them so you can “detect” a scene change? So you can create some kind of interval/timer that runs every so often, take a bitmap “snapshot” of what is in the video display and keep comparing it to the previous shot. I’m wondering if there are any methods in the bitmap class that can help determine the pixel percentage difference (or something like that) between two images captured in this way.

  5. peterd says:

    techpop,

    I think I have an example of grabbing video stills from an VideoDisplay control whenever it encounters a cuePoint event. Regardless, it should be fairly easy if you use the Timer class or maybe listen for the VideoDisplay control’s playheadUpdate event.

    As for bitmap compares, I haven’t used it personally, but the BitmapData class has a compare() function which compares two BitmapData objects (see the Flex 2.0.1 LiveDocs for BitmapData.compare()).

    Peter

  6. judah says:

    A couple questions. Why do you use the Bitmap class instead of an Image class? Also, is there any reason not to use the clone method?

    var image:Bitmap = new Bitmap(source.bitmapData.clone());
    
  7. peterd says:

    judah,

    No reasons, I think this was just the first approach that came to mind.

    Peter

  8. jj says:

    I encountered problem with adding Images to TileList control, and it can be reproduced in your example as well – if you add about 30 copies of the bitmap, catch a scrollbar and start scrolling up and down intensively, after short while, bitmaps start disappearing from random tiles. Also, if i continue, i get this:

    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display::DisplayObjectContainer/removeChild()
    at mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::$removeChild()
    at mx.core::UIComponent/removeChild()
    at mx.controls::SWFLoader/load()
    at mx.controls::SWFLoader/commitProperties()
    at mx.core::UIComponent/validateProperties()
    at mx.managers::LayoutManager/validateClient()
    at mx.controls.listClasses::TileBase/getPreparedItemRenderer()
    at mx.controls.listClasses::TileBase/makeRowsAndColumns()
    at mx.controls.listClasses::TileBase/scrollVertically()
    at mx.controls.listClasses::TileBase/scrollHandler()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.core::UIComponent/dispatchEvent()
    at mx.controls.scrollClasses::ScrollBar/http://www.adobe.com/2006/flex/mx/internal::dispatchScrollEvent()
    at mx.controls.scrollClasses::ScrollThumb/mouseMoveHandler()

    (I have 9,0,115,0 player)

    Do you know any workaround for this? The problem is with TileList and Image controls together, rather than in BitmapData, because when i set direct image address in source field, same problem appears.

    Thanks in advance

  9. betehes says:

    There is a big bug that I’m not able to fix…

    If I fill the TileList with enough objects to make the scrolling button to appear and if I scroll, the images will disappear…

    Am I the only one to have this problem ?

    Is there a way to fix it ?

    Thanks in advance.

  10. martin says:

    @betehes:

    i’m having the exact same problem, very annoying, seems to be a bug in flex for me but i dunno how to fix

  11. martin says:

    @betehes:

    i think i found a solution, changing the itemrenderer to:

    worked in my case

  12. martin says:

    ^^change the image tag in the itemrenderer to this, and link display namespace to “flash.display.*”

    <mx:Image>
        <mx:source>
            <display:Bitmap bitmapData="{data.source}" />
        </mx:source>
    </mx:Image>
    

    sry for double post…seems wordpress has big issues with “greater than” and “smaller than” characters

    • KevinC says:

      I can’t begin to tell you how long I have been looking for a simple solution to rendering a d*mn bitmap like your example…I think I love you. :)
      Thanks Martin. And Peter, than you a well.
      Best,
      KevinC

  13. martin says:

    data.source should be data.image.displayObject in that case

  14. vivek says:

    Hello,

    Is there any way to copy data from SWFLoader instance?

    Thanks

  15. zyanlu says:

    thanks martin.

    your solution is great!

  16. geoff says:

    still cant get this to work with the suggestions at the bottom of the blog

    ^^change the image tag in the itemrenderer to this, and link display namespace to “flash.display.*”

    could you explain this in greater detail

    what does this mean ?

    link display namespace to “flash.display.*”

    thanks

  17. mrm says:

    @geoff

    It means you have to write this:

    xmlns:display="flash.display.*"
    

    at the root tag of your application.

  18. Jason says:

    Martin, you are awesome!! Your solution worked for me. Thanks!!

  19. tgl says:

    When I run the posted demo it works fine, but when I run the code in Flex Builder 3.0.2 SDK 3.2, I run into the scroll problem that everyone describes here, even with the solutions the images disappear during the scroll. Please assist, thank you.

  20. Atul Singh Parihar says:

    its very nice but if you implement with loader then its very usefully
    Thanks
    Atul Singh Parihar

  21. luigi russo says:

    Hello Peter
    can i use prettyPhoto for the zooming effect in my Flex Project…
    Thanks ciaooo from Italy

  22. Devsachin says:

    Hi Peter, Can we implement magnifying glass logic by bitmapData. Do you already have any example for it?
    Please let me know
    :
    Sachin dev

  23. Devsachin says:

    Hi
    I have created a sample here with Flex for
    magnifying glass in flex:

    http://devsachinonflex.blogspot.com/2010/04/magnifying-glass-in-flex.html

    Sachin dev tripathi
    Flex Developer

  24. Brian Bishop says:

    Hi

    Had disappearing images trying with Actionscript only, but got it to work fine with the above example – albeit with more mxml that I would have liked:)

    Thanks a mill for the post (and to Martin for the fix)

    Brian

  25. Kenny says:

    Hi, if I don’t change the bitmap data, why do I need to clone it when duplicating a Bitmap? Can’t I just use the same instance of the BitmapData in multiple Bitmaps?

  26. sanjay says:

    Is there a way to make selection, like magmatic lasso tool around an image and then crop it and save as a .png file in flex.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree