在我的Flex應用程序中,我將圖像加載到Image控件中。加載圖像後,我可以應用像顏色變換或灰度等過濾器,然後我想保存與應用過濾器的圖像。我無法在應用過濾器的情況下保存圖像。使用下面的代碼,圖像可以在沒有濾鏡的情況下保存。但是,如果我使用的是如何用原始尺寸保存圖像?
finalImagedata = ImageSnapshot.captureBitmapData(myImage);
然後,圖像被保存在應用了濾鏡的位置,但是與圖像控件的維度一致。我想用原始尺寸保存圖像。要清楚的是,如果圖像是1000px X 1000px,並顯示在300px X 300px圖像控件中,則在保存時我仍然希望將圖像保存爲1000px X 1000px二維。怎麼做?我也試圖直接從像下面這樣的圖像控件內容中獲取BitmapData,但這似乎不起作用。任何幫助傢伙?
finalImagedata = Bitmap(myImage.content).bitmapData;
var filtersArray:Array = new Array;
private var finalImagedata:BitmapData;
private function loadImage(url:String):void
{
var request:URLRequest = new URLRequest(url);
var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, image_completeHanlder);
imageLoader.load(request);
}
private function image_completeHanlder(event:Event):void
{
var bmd:BitmapData = Bitmap(event.currentTarget.content).bitmapData;
var bmpMy:Bitmap = new Bitmap(bmd);
myImage.data=bmpMy;
}
protected function button1_clickHandler(event:MouseEvent):void
{
loadImage("http://localhost/main/image.jpg");
}
protected function prepareFinalImage():void
{
finalImagedata = new BitmapData(myImage.content.width, myImage.content.height, true);
finalImagedata.draw (myImage.content);
}
protected function btnSave_clickHandler(event:MouseEvent):void
{
prepareFinalImage();
var encoder:Object = new Object();
var defaultName:String;
var je:*;
je = new JPGEncoder(100);
encoder.encode = je.encode;
defaultName = "myImage.jpg";
var imageBytes:ByteArray = encoder.encode(finalImagedata);
var fr:FileReference = new FileReference();
fr.save(imageBytes,defaultName);
}
protected function btnGrayScale_clickHandler(event:MouseEvent):void
{
var red:Number = 0.3086; // luminance contrast value for red
var green:Number = 0.694; // luminance contrast value for green
var blue:Number = 0.0820; // luminance contrast value for blue
var cmf1:ColorMatrixFilter = new ColorMatrixFilter([red, green, blue, 0, 0, red, green, blue, 0, 0, red, green, blue, 0, 0, 0, 0, 0, 1, 0]);
filtersArray.push(cmf1);
myImage.filters = filtersArray;
}
你的意思是「如何用它保存圖像是原始尺寸」?如果沒有,那麼你應該擺脫撇號,並寫下「如何保存圖像的原始大小」,而不是 – Vivi 2011-05-08 08:00:20