How do you load a bitmap file into a BitmapData object?
Asked Answered
C

5

16

In Flash, the BitmapData object can be used to store bitmaps in RAM, you can later draw them to a MovieClip using the beginBitmapFill() method.

How do you load an external bitmap file (.jpg) into a BitmapData object?

Even AS3 code would be helpful.

Cherellecheremis answered 31/3, 2009 at 17:32 Comment(2)
I don't really understand why you took your own answer instead of Cotton's.Adz
Because I wanted an AS2 solution. Cotton's is AS3.Cherellecheremis
B
42

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;
}
Bot answered 1/4, 2009 at 6:8 Comment(2)
Just a note. You can't use a URLLoader for thisBiota
Another note. Loader adds some info to the bitmap, so trying to change some attributes like name directly from event.target.content . But as the OP creating a new bitmap is good.Biota
S
9

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;
Selfabuse answered 17/8, 2011 at 15:0 Comment(1)
No, LoaderInfo.content is a DisplayObject. All Bitmaps are DisplayObjects, but not all DisplayObjects are Bitmaps, hence the cast. By removing the casts, you are working around the type safety feature of the ActionScript language.Berk
C
6

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);
}
Cadaverous answered 6/2, 2012 at 8:19 Comment(0)
C
5

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.

Cherellecheremis answered 31/3, 2009 at 17:34 Comment(0)
K
1

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);
Kamchatka answered 8/11, 2009 at 20:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.