views:

52

answers:

3

hi, i'm using jpeg encoder script. in the script example, a movie clip is converted to jpeg. differently i want to convert the stage but only some part of it such as x:320-500, y:0-600. is it possible?

function createJPG(m:MovieClip, quality:Number, fileName:String)
{
    var jpgSource:BitmapData = new BitmapData (m.width, m.height);
    jpgSource.draw(m);

    var jpgEncoder:JPGEncoder = new JPGEncoder(quality);
    var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);     
    ...
}

create(partOfStage,90,"name");
A: 

I don't know if it's the best solution but:
You can create another BitmapData and copyPixel to it from original BitmapData in a loop.

for(var x:int = startX; x < endX; x++){
    for(var y:int = startY; y < endY; y++){
        newBD.setPixel(x, y, originalBD.getPixel(startX+x, startY+y));
    }
}

EDIT: actually.. never mind.. there is already a function for that http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/display/BitmapData.html#copyPixels%28%29

Antriel
+1  A: 

Hi,

Here is a quick example:

graphics.beginFill(0x000000);
graphics.drawRect(0, 0, 100, 100);
graphics.endFill();
graphics.lineStyle(0,0xFF0000);
graphics.moveTo(0, 0);
graphics.lineTo(100, 100);
graphics.beginFill(0xFF0000);
graphics.drawRect(95, 95, 5, 5);
graphics.endFill();
//          
var captureArea:Rectangle=new Rectangle(80,80,100,100);         
var bitmapData1:BitmapData=new BitmapData(stage.stageWidth, stage.stageHeight,true,0);
var bitmapData2:BitmapData=new BitmapData(captureArea.width, captureArea.height,false,0x00FF00);
bitmapData1.draw(stage, null, null, null);
bitmapData2.copyPixels(bitmapData1, captureArea, new Point());  
var bitmap:Bitmap=new Bitmap(bitmapData2);
bitmap.x=100;
bitmap.y=100;
addChild(bitmap);

Hope it will help you !

OXMO456
+4  A: 

The answers already posted will probably work, but there's a simpler way. Matrix is your friend here.

This also uses less resources: only one BitmapData and only as big as it needs to be (that is, as big as the area to crop):

//  x:320-500, y:0-600
//  --> x: 320, y: 0, width: (500-320), height: (600 - 0)
var cropArea:Rectangle = new Rectangle(320,0,180,600);
var mat:Matrix = new Matrix();
mat.translate(-cropArea.x,-cropArea.y);
var snapshot:BitmapData = new BitmapData(cropArea.width,cropArea.height);
snapshot.draw(stage,mat);

You can avoid the Rectangle but I'd leave it as it's more readable, in my opinion.

Juan Pablo Califano
you're right ! : )
OXMO456