PDA

View Full Version : question about the BitmapData draw() method



SquirtGun
August 17th, 2007, 07:58 PM
Example code:


var bitmap:BitmapData = new BitmapData(100, 100, true, 0x00ffffff)
bitmap.draw(sprite);

Is there a way to define the starting x,y point when drawing a sprites contents to a BitmapData object. I want to only copy a small section of the sprite, that is not the top left most corner.

Thanks for any help.

~squirt

Templarian
August 17th, 2007, 08:30 PM
Theres no way to do it... you have to use the graphics object.

if i could delete this post i would have, i answered your question then notice i read it wrong.

Reider
August 18th, 2007, 06:45 AM
After you created a bitmapdata fom your sprite you can copy the selected content with copyPixels to another bitmapdata on the x,y specified by the destPoint parameter.

Dazzer
August 19th, 2007, 08:11 AM
...

Sorry what are you guys talking about?

http://livedocs.adobe.com/flex/2/langref/flash/display/BitmapData.html#draw()

Use the matrixTransform and clipRect properties.

Those properties are applied to the Sprite you are drawing FIRST, before it draws. Those are not permanent, so you don't have to worry.

joshchernoff
September 3rd, 2007, 03:54 AM
I think I all most have it, but it wont work.

I use the corlib JPGEncoder class with this as well to make a jpg of a sprite.

But the problem is that the image inside the sprite has been centered to make it's index of x:0, y:0, it's center.

What happens with the draw it that the center of the sprite becomes the topLeft of the new bitmap and it clips off every thing -x, -y, of the center.

here's what I got now. I'm trying to use a Rectangle to get the proper index of the bitmap so that the draw method sees all of the image.




import com.adobe.images.JPGEncoder;

var test:Loader = new Loader();
var myPreviewLoader:Loader = new Loader();

test.load(new URLRequest("thumbair.jpg"));
test.contentLoaderInfo.addEventListener(Event.COMP LETE, onCom);

function onCom(e:Event):void {
var sprite:Sprite = new Sprite();
sprite.y = (stage.stageHeight >> 1);
sprite.x = (stage.stageWidth >> 1);
addChild(sprite);

test.x -= test.width >> 1;
test.y -= test.height >> 1;
sprite.addChild(test);

var rect:Rectangle = Rectangle(sprite.getBounds(test));

rect.topLeft = new Point(-40, -30);
var myBitmapSource:BitmapData = new BitmapData (test.width, test.height, false, 0xFF0000);

myBitmapSource.draw(sprite, null, null, null, rect);
var myEncoder:JPGEncoder = new JPGEncoder(100);
var myCapStream:ByteArray = myEncoder.encode ( myBitmapSource );
myPreviewLoader.loadBytes( myCapStream );
addChild(myPreviewLoader);

}

Rezmason
September 3rd, 2007, 12:29 PM
Try this out, Josh.

import com.adobe.images.JPGEncoder;

var test:Loader = new Loader;
var myPreviewLoader:Loader = new Loader;
var matrix:Matrix = new Matrix;
var myBitmapSource:BitmapData;
var rect:Rectangle;

test.load(new URLRequest("thumbair.jpg"));
test.contentLoaderInfo.addEventListener(Event.COMP LETE, onCom);

function onCom(e:Event):void
{
rect = Rectangle(test.getBounds(test));
matrix.tx = -rect.x;
matrix.ty = -rect.y;
myBitmapSource = new BitmapData (test.width, test.height, false, 0xFF0000);

myBitmapSource.draw(testbed, matrix);
var myEncoder:JPGEncoder = new JPGEncoder(100);
var myCapStream:ByteArray = myEncoder.encode ( myBitmapSource );
myPreviewLoader.loadBytes( myCapStream );
addChild(myPreviewLoader);
}