Saturday, August 4, 2007

Duplicating images using the Bitmap and BitmapData classes

original author : Peter deHaan

original source : Peter deHaan's blog

== quote from his blog ==
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"?>

<
mx:Application 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>

2 comments:

peterd said...

I dont get it. Why do you keep copy and pasting my entire posts and put them on your site?

I'm flattered you seem to find my posts useful, but taking the ENTIRE post?

Unknown said...

sorry about that, i just saw your comments, although i do include source "here" at the top to link to your original post, i should mention the original author.

I grab useful posts from internet and put them in my blog to easy access, but I really
should include author's name.