有关正确复制图片的方法
拖动鼠标生成选择框的时候是正确的,可以在左上角生成复制的内容,但是生成选择框之后拖动的话,怎样正确复制的图像内容?
代码如下:
//stage.align=StageAlign.TOP_LEFT;
stage.scaleMode=StageScaleMode.NO_SCALE;
var selectBox:Sprite = new Sprite();//生成选择框
var recordX:Number;//记录初始x坐标
var recordY:Number;//记录初始y坐标
var endX:Number;//记录结束x坐标
var endY:Number;//记录结束y坐标
var moveX:Number;//记录移动之后x坐标
var moveY:Number;//记录移动之后y坐标
var myImage:Bitmap;//显示图片的容器
var dragFlag:Boolean=false;//用于判断selectBox是否正在拖动
var clickPoint:Point;
initStage();
function initStage() {
myImage=new Bitmap();
addChild(myImage);
}
bg.addEventListener(MouseEvent.MOUSE_DOWN,startDraw);
selectBox.addEventListener(MouseEvent.MOUSE_DOWN,dragSelectBox);//拖动selectBox事件
selectBox.addEventListener(MouseEvent.MOUSE_UP,stopDragSelectBox);
function dragSelectBox(_evt:MouseEvent):void {
_evt.currentTarget.startDrag();
dragFlag=true;
}
function stopDragSelectBox(_evt:MouseEvent) {
if (dragFlag==true) {
createJPG(mc_image,selectBox,selectBox.localToGlobal(clickPoint).x,selectBox.localToGlobal(clickPoint).y);
selectBox.stopDrag();
dragFlag = false;//
}
}
//开始绘制
function startDraw(_evt:MouseEvent):void {
stage.addEventListener(Event.ENTER_FRAME,drawSelectBox);
recordX = mouseX;
recordY = mouseY;
clickPoint = new Point(recordX,recordY);
trace("sbssb")
}
//绘制选择框
function drawSelectBox(_evt:Event):void {
selectBox.x=selectBox.y=0;//让坐标回去
selectBox.graphics.clear();
selectBox.graphics.lineStyle(1,0x000000);
selectBox.graphics.beginFill(0xffffff,0.7);
selectBox.graphics.drawRect(recordX,recordY,mouseX-recordX,mouseY-recordY);
selectBox.graphics.endFill();
addChild(selectBox);
stage.addEventListener(MouseEvent.MOUSE_UP,endDraw);// 鼠标在舞台上放开时的事件.
}
//绘制结束
function endDraw(_evt:MouseEvent):void {
stage.removeEventListener(Event.ENTER_FRAME,drawSelectBox);
selectBox.stopDrag();
dragFlag = false;//
endX = mouseX;
endY = mouseY;
createJPG(mc_image,selectBox,Math.min(recordX,endX),Math.min(recordY,endY));
stage.removeEventListener(MouseEvent.MOUSE_UP,endDraw);// 鼠标在舞台上放开时的事件.
}
function createJPG(m:MovieClip,box:Sprite,originX:Number,originY:Number) {
removeChild(myImage);
var jpgSource:BitmapData= new BitmapData (m.width, m.height);
jpgSource.draw(m);
var rect:Rectangle = new Rectangle(originX,originY,box.width,box.height);
//var pt:Point = new Point(box.x, box.y);
var pt:Point = new Point(0, 0);
var jpgOut:BitmapData= new BitmapData(box.width, box.height);
jpgOut.copyPixels(jpgSource, rect, pt);
myImage = new Bitmap(jpgOut);
addChild(myImage);
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |