簡體   English   中英

(Flex)如何在沒有滾動條的情況下獲取整個組件的圖像快照?

[英]( Flex ) How can we get an imagesnapshot of the entire component without scrollbars?

我可以拍攝組件的快照。 但問題是使用滾動條可以使組件變大。 保存的圖像具有滾動條(僅保存可見區域)。 我需要的是我希望將整個組件保存為圖像。

當我們使用FlexPrintJob打印組件時,可以使用此確切功能,其中通過設置FlexPrintJobScaleType.NONE。

但在我的情況下,我希望使用ImageSnapShot(不通過FlexPrintJob)保存它。

謝謝Advance,Sriss

我以為我知道怎么做,但似乎有很多尷尬的問題。 我得到了它的工作,但它並不好。 :-(也許你可以改進它。

這是示例應用程序的代碼。 以下是MyCanvas類的代碼。 單擊該按鈕時,應繪制Canvas容器但沒有滾動條的圖像。

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:my="*">
        <mx:Script><![CDATA[
            import flash.display.BitmapData;
            import flash.events.Event;
            import mx.containers.Canvas;
            import mx.graphics.ImageSnapshot;
            import flash.geom.Matrix;
            import mx.core.ScrollPolicy;

            public function onclick():void
            {
                var bitmapData:BitmapData = ImageSnapshot.captureBitmapData(canvas);
                canvas.addEventListener("BitMapReady", onBitMapReady);
                canvas.horizontalScrollPolicy = ScrollPolicy.OFF;
                canvas.CreateBitMapData();
            }
            private function onBitMapReady(e:Event):void
            {
                DrawBitmapDataAt(canvas.bitMapData, 100, 100);
                canvas.removeEventListener("BitMapReady", onBitMapReady);
                canvas.horizontalScrollPolicy = ScrollPolicy.AUTO;
            }
            private function DrawBitmapDataAt(bitmapData:BitmapData,x:int,y:int):void
            {
                var matrix:Matrix = new Matrix();
                matrix.tx = x;
                matrix.ty = y;
                box.graphics.lineStyle(0,0,0);
                box.graphics.beginBitmapFill(bitmapData, matrix, false);
                box.graphics.drawRect(x,y,bitmapData.width,bitmapData.height);
            }
        ]]></mx:Script>
        <mx:Box id="box">
            <my:MyCanvas width="50" height="50" backgroundColor="white" id="canvas">
                <mx:Button label="Hello" click="onclick()" />
            </my:MyCanvas>
        </mx:Box>
    </mx:Application>

MyCanvas類:

package  
{
    import flash.events.Event;
    import flash.events.TimerEvent;
    import mx.containers.Canvas;
    import flash.display.BitmapData;
    import mx.core.ScrollPolicy;
    import mx.graphics.ImageSnapshot;
    import flash.utils.Timer;

    public class MyCanvas extends Canvas
    {
        public var bitMapData:BitmapData;
        private var creatingBitMap:Boolean = false;
        private var timer:Timer;

        public function CreateBitMapData():void
        {
            this.horizontalScrollPolicy = ScrollPolicy.OFF;
            creatingBitMap = true;
        }
        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            super.updateDisplayList(unscaledWidth, unscaledHeight);
            if (creatingBitMap == true && this.horizontalScrollBar == null)
            {
                bitMapData = ImageSnapshot.captureBitmapData(this);
                this.dispatchEvent(new Event("BitMapReady"));
                creatingBitMap = false;
                timer = new Timer(10);
                timer.addEventListener(TimerEvent.TIMER, onTimer);
                this.width += 1;
                timer.start();
            }
        }
        private function onTimer(e:TimerEvent):void
        {
            this.width -= 1;
            trace("timer");
            timer.removeEventListener(TimerEvent.TIMER, onTimer);
            timer.stop();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM