Duplicating images using the Bitmap and BitmapData classes
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.
<?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.
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.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
28 Responses to Duplicating images using the Bitmap and BitmapData classes
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


Good sample of bitmap operation, thank your for sharing experience!
Very nice! I wait for the next example.
Good one! Thanks.
~Tushar
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.
techpop,
I think I have an example of grabbing video stills from an VideoDisplay control whenever it encounters a
cuePointevent. Regardless, it should be fairly easy if you use the Timer class or maybe listen for the VideoDisplay control’splayheadUpdateevent.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 forBitmapData.compare()).Peter
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?
judah,
No reasons, I think this was just the first approach that came to mind.
Peter
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:
(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
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.
@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
@betehes:
i think i found a solution, changing the itemrenderer to:
worked in my case
^^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
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
data.sourceshould bedata.image.displayObjectin that caseHello,
Is there any way to copy data from SWFLoader instance?
Thanks
thanks martin.
your solution is great!
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
@geoff
It means you have to write this:
at the root tag of your application.
Martin, you are awesome!! Your solution worked for me. Thanks!!
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.
its very nice but if you implement with loader then its very usefully
Thanks
Atul Singh Parihar
Are u belonging to rewa Mp India??? :)
Hello Peter
can i use prettyPhoto for the zooming effect in my Flex Project…
Thanks ciaooo from Italy
Hi Peter, Can we implement magnifying glass logic by bitmapData. Do you already have any example for it?
Please let me know
:
Sachin dev
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
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
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?
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.