How do you load a bitmap file into a BitmapData object?

假如想象 提交于 2019-11-29 01:25:34
Cotton

AS3 code to load a PNG and "get" its bitmapData

var bitmapData:BitmapData;

var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
    loader.load(new URLRequest("../lib/img.png"));

function onComplete (event:Event):void
{
    bitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;
}

Refering to the first post by cotton.

Actually each image is a bitmap so all you need to do is

bitmapData = event.target.content.bitmapData  

instead of

bitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;

taking from cotton and will, this will display the bitmap after it is loaded:

import flash.display.Bitmap;
import flash.display.BitmapData;

var bitmapData:BitmapData;
var bmVis:Bitmap;

var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
    loader.load(new URLRequest("put url here"));

function onComplete (event:Event):void
{
    trace("loaded!");
    bitmapData = event.target.content.bitmapData;
    bmVis = new Bitmap(bitmapData);
    this.addChild(bmVis);
}

You have to load the external file (.jpg) into a MovieClip and wait for it to load.

Then take a snapshot of the MovieClip that contains the external file, into your BitmapData object.

myBitmap = new BitmapData(myMC._width, myMC._height,true,0x00FFFFFF)

myBitmap.draw( myMC)

See Introducing the Image API in Flash 8.

GUI phase:
Load the image into the library (file -> import -> library).
In the library browser, right-click and hit "properties", notice that it is X by Y pixels.
Open up the "advanced" tab.
Select "export for Action Script". Export as myImg

Actionscript phase:

import flash.display.BitmapData;
import flash.display.Bitmap; //needed?
..........
var myPic:BitmapData = new myImg(X,Y);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!