Sun 25 Jan 2009
Drawing BitmapData to a Flex Canvas
Posted by braincog under Flex/Flash/ActionScript
[14] Comments
I was having a hard time drawing a Flex BitmapData object (or Bitmap) to a canvas. Since neither of these objects are UIComponents, they cannot be added directly as children to a Canvas using the AddChild method-you’ll get a runtime error. After poking around the web, I quickly found a suggestion to simply add the Bitmap to a UIComponent like Sprite or Image and then add that to the Canvas. Like this:
var myBitmap:Bitmap = new Bitmap(myBitmapData); var myImage:Image = new Image(); myImage.addChild(myBitmap); myCanvas.addChild(myImage);
So this method will get your Bitmap/BitmapData onto the canvas. But then we have a new problem: if the image is larger than the canvas, the image will extend outside the limits of the canvas, even if the canvas has scrollbars enabled (the large image does not even activate the canvas scrollbars). This was no good for me. I wanted to be able to scroll the canvas to view the entire image.
The solution that worked better than the AddChild method above was to instead set the Image.source property to the Bitmap. Like this:
var myBitmap:Bitmap = new Bitmap(myBitmapData); var myImage:Image = new Image(); myImage.source = myBitmap; myCanvas.addChild(myImage);
Presto! The BitmapData is displayed in the canvas and we get nice scrollbars.
14 Responses to “ Drawing BitmapData to a Flex Canvas ”
Trackbacks & Pingbacks:
-
Bitmap auf einem Flex Canvas scrollt nicht?…
in einem Flex3-Projekt sollen die Bitmaps jetzt nicht mehr, wie vorher, zentriert in ihren Rahmen gecroppt stehen, sondern mit Scrollbalken. Mein alter Weg war, wie in der Adobe Referenz empfohlen, eine UIComponent zu erzeugen, der ich die Bitmap …
Thanks,
just what I was looking for.
You are the best! xdxd.
I´ve been making tests, using addChild from a Bitmap directly to a UIComponent…, and is strange, because sometimes works, and anothers didn´t work… If i don´t give to UIComponent all the parameters, don´t work anything, but i used a component .mxml, and sometimes yes, …
Thanks friend!. Best regards from Spain,
Montxi
Huge thanks !
This solutions allowed me use BulkLoader api in Flex. I didn’t manage to add my retrieved bitmap from Bulkloader instance onto a Canvas.
Thanks !
isnan
Very useful information braincog.Thanks for sharing this.One strange behaviour I came to know while implementing your logic that if you put the width and height of canvas same as the width and height of your AIR Window then in that case it doesn’t show horizontal scroll bars.Any guesses why so ? I am using Air 1.5 runtime,FYI.
Hi
Nice one. Good post, i needed the image.source = bitmap.
You are best, i am looking for this. Thank you!
Thanks for this…
Hi there,
I’m very newbie to Flex. But i have big problem have to decide using by Flex. Could you show me full source?
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.
Bingo, just the right answer for my question,
thanks for posting
Hmm it appears like your website ate my first comment (it was super long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well am an aspiring blog blogger but I’m still new to the whole thing. Do you have any recommendations for beginner blog writers? I’d really appreciate it.
tried and tested.. works well! thanks!
Excellent post!
Extremely useful to me.
I was using a fixed canvas with a fixed Image inside and when I needed to change the image I just updated the source property of Image.
That was getting no scrollbars at all. Now I can see them!
Thanks a lot!