Skip to main content
Known Participant
May 18, 2014
Answered

how to copyPixels from an Image

  • May 18, 2014
  • 2 replies
  • 1661 views

Hello.

My question is ..... well the Tittle of the post says it all.

How do i copyPixels from a image that i have onstage?

I know that when you use .draw it is simple - 1st thing you do is pass the source of the item you want to copy from, no matter if it is bitmapData, Bitmap, Sprite, MC or something else,

but  when with copyPixels it is asking specifically for bitmapData..... and that is my problem, what if my source is a MovieClip dragged from the library to the stage.

I did read the http://help.adobe.com referance for the copyPixels but there they create directly the bitmapData like this var bmd2:BitmapData = new BitmapData(80, 40, false, 0x0000CC44);

and then copy from it. But how do you copyPixels from a MC .

Im trying something like this

var newPieceImage1:Bitmap=new Bitmap(new BitmapData(100, 100));

newPieceImage1.bitmapData.copyPixels(movingMC.bitmapData, new Rectangle(0, 0, 100, 100), new Point(0,0));

where the  movingMC is actually the picture from the library

Hope you understand my question.

PS: i also would like to ask if i want to copyPixels from a mc with Moving objects in it, how do i do it ?

This topic has been closed for replies.
Correct answer kglad

because I think it is the better way of displaying what is happening  (what is moving) on an element that is taking that data from another element.

( also i have uploaded my code and the swf on this link http://wonderfl.net/c/4qie/edit so now it doesnt need to be downloaded.

PS: there wasnt a way to upload the actual movingMC so I had to create it dynamically ( the createMainMC represents it but it is actually a more complex MovieClip that cant be just coded ( it has like some cars moving, people moving , etc. ) ).

I`ve tried to make things as simple as to show what Im currently trying to do.

But as I have said, when i test this code ( and have the real/comlpexed movingMC ) it is starting to loose performance.

So am I supposed to use blitting with that or something else ?


this, 'because I think it is the better way of displaying what is happening  (what is moving) on an element that is taking that data from another element', isn't a compelling justification.  that doesn't explain what you think is better.

i'm not trying to give you a hard time.  but i suspect you're making your app much more complex for no good reason.

in general, it's not better to create a sequence of bitmaps instead of using a movieclip.

the only justfication i know of is to improve performance.  but unless you're publishing for a mobile device performance isn't usually an issue.

in any case, in exchange for improved performance (ie, blitting) you increase your apps complexity and you increase its memory use.  and it's easy to exceed the available system memory.

on the other hand, perhaps you just want to learn how to blit or partially-blit an app.  that's a good reason to pursue this approach.

if so, make sure you're not going to exceed system memory:  how large (100 x 100?) is movingMC?  how many frames does it contain?  will you need to create transformed bitmaps (because you're rotating/changing alpha/ etc) also?

2 replies

Participating Frequently
May 18, 2014

You can do whatever you want with normal movieclips in flash - draw, dynamic text, import images - then turn the movieclips into a bitmap for gpu optimization.  When you're creating the movieclips, or when creating bitmaps out of the movieclips, you get a cpu/gpu hit.  So don't so it during play, do it during loading screen (like in games).

Here's a function that returns a bitmap of a movieclip:

bmpMain = convertBMP(mcTest.sTest, 752, 115);

mcTest.removeChild(mcTest.sTest);

mcTest.sTest = null;

mcTest.addChild(bmpMain);

private function convertBMP(pClip: MovieClip, pWidth: int, pHeight: int): Bitmap {

  var bd: BitmapData = new BitmapData(pWidth, pHeight, true, 0x00000000);

  bd.draw(pClip, null, null, null, null, true);

  aBitmaps.push(bd);

  var bmp: Bitmap = new Bitmap(bd, "auto", false);

  bmp.smoothing = true;

  return bmp;

}

Remove the original moviclip and all references to it.  It pushes the bitmap into an array for reference:  aBitmaps   If you want to reference the bitmap directly, add it to a container movieclip.

kglad
Community Expert
Community Expert
May 18, 2014

do you have an actionscript reference to your on-stage image?

if not, you need one.  how you do that depends on how the object was added to your stage.

if yes, use that reference to create your bitmapdata instance.

p.s.  to copy pixels from any object you need to create a bitmapdata instance with those pixels.

Known Participant
May 18, 2014

Well my library mc is exported for ActionScript with the name of Box


my whole code is this

import flash.display.MovieClip;

import flash.display.Bitmap;

import flash.display.BitmapData;

var movingMC:MovieClip = new Box();

movingMC.x = this.x;

movingMC.y = this.y;

addChild( movingMC );

var newPieceImage1:Bitmap=new Bitmap( new BitmapData(100, 100) );

newPieceImage1.bitmapData.copyPixels( movingMC.bitmapData, new Rectangle(0, 0, 100, 100), new Point(0,0) );

var sectionFromPicture_1:Sprite = new Sprite();

sectionFromPicture_1.addChild( newPieceImage1 );

sectionFromPicture_1.x = 300;

sectionFromPicture_1.y = 300;


var sectionFromPicture_1:Sprite = new Sprite();

sectionFromPicture_1.addChild( newPieceImage1 );

sectionFromPicture_1.x = 300;

sectionFromPicture_1.y = 300;


but it gives me  : TypeError: Error #2007: Parameter sourceBitmapData must be non-null.

what Do i do ? convert the movingMC into bitmapData?

kglad
Community Expert
Community Expert
May 18, 2014

i think (but am not sure), the title of your thread is misleading.  it doesn't appear that you're trying to copy pixels from an image.  you want to copy pixels from a multi-frame movieclip, correct?  and that's your only issue, correct?

if yes and yes, first create multiple bitmapdata instances of each frame of your multi-frame movieclip using a loop like enterframe.

second, movieclips don't have a bitmapData property.  so this movingMC.bitmapData will be null.

to create a bitmapdata instance from a movieclip, use the draw() method.