View comments | RSS feed

draw (BitmapData.draw method)

public draw(source:Object, [matrix:Matrix], [colorTransform:ColorTransform], [blendMode:Object], [clipRect:Rectangle], [smooth:Boolean]) : Void

Draws a source image or movie clip onto a destination image, using the Flash Player vector renderer. You can use Matrix, ColorTransform, BlendMode objects, and a destination Rectangle object to control how the rendering performs. Optionally, you can specify whether the bitmap should be smoothed when scaled. This works only if the source object is a BitmapData object.

This method directly corresponds to how objects are drawn using the standard vector renderer for objects in the authoring tool interface.

A source MovieClip object does not use any of its on-stage transformations for this call. It is treated as it exists in the library or file, with no matrix transform, no color transform, and no blend mode. If you want to draw the movie clip by using its own transform properties, you can use its Transform object to pass the various transformation properties.

Availability: ActionScript 1.0; Flash Player 8

Parameters

source:Object - The BitmapData object to draw.

matrix:flash.geom.Matrix [optional] - A Matrix object used to scale, rotate, or translate the coordinates of the bitmap. If no object is supplied, the bitmap image will not be transformed. Set this parameter to an identity matrix, created using the default new Matrix() constructor, if you must pass this parameter but you do not want to transform the image.

colorTransform:flash.geom.ColorTransform [optional] - A ColorTransform object that you use to adjust the color values of the bitmap. If no object is supplied, the bitmap image's colors will not be transformed. Set this parameter to a ColorTransform object created using the default new ColorTransform() constructor, if you must pass this parameter but you do not want to transform the image.

blendMode:Object [optional] - A BlendMode object.

clipRect:flash.geom.Rectangle [optional] - A Rectangle object. If you do not supply this value, no clipping occurs.

smooth:Boolean [optional] - A Boolean value that determines whether a BitmapData object is smoothed when scaled. The default value is false.

Example

The following example shows how to draw from a source MovieClip instance to a BitmapData object.

import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.geom.ColorTransform;

var myBitmapData:BitmapData = new BitmapData(100, 80, false, 0x00CCCCCC);

var mc_1:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
mc_1.attachBitmap(myBitmapData, this.getNextHighestDepth());

var mc_2:MovieClip = createRectangle(50, 40, 0xFF0000);
mc_2._x = 101;

var myMatrix:Matrix = new Matrix();
myMatrix.rotate(Math.PI/2);

var translateMatrix:Matrix = new Matrix();
translateMatrix.translate(70, 15);

myMatrix.concat(translateMatrix);

var myColorTransform:ColorTransform = new ColorTransform(0, 0, 1, 1, 0, 0, 255, 0);
var blendMode:String = "normal";

var myRectangle:Rectangle = new Rectangle(0, 0, 100, 80);
var smooth:Boolean = true;

mc_1.onPress = function() {
    myBitmapData.draw(mc_2, myMatrix, myColorTransform, blendMode, myRectangle, smooth);
}

function createRectangle(width:Number, height:Number, color:Number):MovieClip {
    var depth:Number = this.getNextHighestDepth();
    var mc:MovieClip = this.createEmptyMovieClip("mc_" + depth, depth);
    mc.beginFill(color);
    mc.lineTo(0, height);
    mc.lineTo(width, height);
    mc.lineTo(width, 0);
    mc.lineTo(0, 0);
    return mc;
}

Version 8

Comments


Charles Parcell said on Sep 30, 2005 at 11:08 AM :
Here is a bare bones example written by Andreas Weber off the FlashCoder list.

// 'snapshot'

import flash.display.BitmapData;

// create a clip with visual content
original = this.createEmptyMovieClip("orig", this.getNextHighestDepth());
original.opaqueBackground = 0xFF0000;
original.lineTo(100,100);

// create a BitmapData object with the same size
var myBitmapData:BitmapData = new BitmapData(original._width,
original._height);

// attach the Bitmap to a clip so that it will be rendered on screen
var copy:MovieClip = this.createEmptyMovieClip("cop",
this.getNextHighestDepth());
copy.attachBitmap(myBitmapData, this.getNextHighestDepth());
copy._x = 300;

// take a 'snapshot'
myBitmapData.draw(original);
No screen name said on Oct 12, 2005 at 9:06 AM :
It seems that I can't retrieve the image information from an image loaded from another server, is this a seccurity issue?
the code:

var bmp;
var loadMC = this.createEmptyMovieClip("preLoad",this.getNextHighestDepth());
var tmpMC = loadMC.createEmptyMovieClip("tmp",loadMC.getNextHighestDepth());
tmpMC.loadMovie(url);
loadMC.onEnterFrame = function() {
if(tmpMC._width>0) {
bmp = new BitmapData(tmpMC._width, tmpMC._height, false, 0xFFFFFF);
bmp.draw(tmpMC);
loadMC.removeMovieClip();
delete this.onEnterFrame;
}
}

this works great with images from the same server as the swf file, but with an external image, the width and height of the bitmapData object is right, but there is no pixel information...

Greets,

Cay Garrido
Mister Neb said on Nov 3, 2005 at 2:10 PM :
Cay is correct: you are not allowed to use BitmapData.draw to retrieve pixels containing media from a domain other than your own. This limitation should be documented.

If you control both domains in a cross-domain situation, you can use System.security.allowDomain to permit BitmapData.draw to work across domains. Say that domain A is the one calling BitmapData.draw, and domain B is the one from which the media was loaded. If the media from domain B is a SWF, it can call System.security.allowDomain("domainA.com"), and then the SWF from domain A can call BitmapData.draw.

Note that there is one problem with this permission mechanism: as of player 8, SWFs tend to speak only for themselves when they call allowDomain, and not for their entire domains. This means there's no obvious way to permit BitmapData.draw to work across domains when the out-of-domain media being rendered is a JPG, GIF, or PNG. The workaround in this situation is to embed your JPG, GIF, or PNG in a SWF, and have that wrapper SWF call allowDomain. Macromedia is aware of this limitation and we hope to fix it in a future release.
No screen name said on Nov 15, 2005 at 2:10 PM :
Problem with draw exists for cross-domains.
Decision for JPG, GIF, or PNG is
Publish Settings/Flash/Lockal playback security -> Access local files only.
Its work for this:
function LoadImage(FileName:String) {
var mclListener:Object = new Object();
mclListener.onLoadInit = function(target_mc:MovieClip):Void {
if (target_mc._width) {
var w:Number = target_mc._width;
var h:Number = target_mc._height;
bmp = new BitmapData(w,h, true, 0xaa3344);
bmp.draw(target_mc);
target_mc.removeMovieClip();
updateAfterEvent();
}
};
var image_mcl:MovieClipLoader = new MovieClipLoader();
image_mcl.addListener(mclListener);
image_mcl.loadClip(FileName, By);
}
where FileName is local file (JPG, GIF, or PNG ).
No screen name said on Nov 21, 2005 at 5:54 AM :
Mister Ned, is there any particular reason for this, or is it a bug?
I solved the problem by passing the URL of the image to a PHP file in my own domain, with a code like this:
loadImage.php?file=http://www.domain.com/image.jpg
<?php
header("Content-Type: image/png");
readfile($file);
?>
it works fine with PNGs, JPEGs and GIFs.

Cheers,
Cay
HedgePedge said on Dec 3, 2005 at 11:46 PM :
I've downloaded a bunch of flash8 experiments to learn from them.
So far in every example i've seen
'source' is not a BitmapData Object, but it is a MovieClip.
I always wonder how the people who made the experiments could have
figured that out.
BukuMartian said on Dec 21, 2005 at 8:24 PM :
if you draw() a textField onto bitmapData,

and you use any blendMode other than 'normal'

flash will crash.
Macintosh
FredzCode said on Feb 7, 2006 at 1:20 AM :
Mister Ned,

Just wanted to let some know, I've elaborated how this mechanism of cross-domain loading JPG, GIF, PNG and FLV assets can be done without actually embeding them individually in SWF files. The idea is host a little library on the server that has the assets for it to render your assets for you. (maybe it can be simplified, haven't looked into it yet)

Here's the link:
http://flashforever.homeip.net/blog/?p=4#comment-31
(sooner or later I might actually post a whole post on this)

Hope this helps.

Cheers
Fredz./
occhio said on Jun 22, 2006 at 2:35 AM :
ASP

<%
url=Request("imageUrl")
Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
Http.Open "GET", url, False
Http.Send
Response.BinaryWrite Http.ResponseBody
Set Http = Nothing
%>
johnnystorm0 said on Nov 9, 2006 at 7:26 AM :
Blend Modes:
* normal
* hardlight
* overlay
* alpha
* invert
* subtract
* add
* difference
* darken
* lighten
* screen
* multiply
* layer
No screen name said on Jan 23, 2007 at 4:35 PM :
Pertaining to the cross-domain file loading using the bitmap.draw()...
Does 'a future release' include Flash 9, or does it mean some other future
release? All work arounds are based on moving the file to the hosting
server. What if the bandwidth is an issue? I have only found this entry and
one other on the web that address this issue. One is on a forum, and the
other in a comments on a help section. Is their any official documentation
that I can show to my client or is this it?
KNovak23 said on Jan 31, 2007 at 3:14 PM :
As soon as I add in the parameters (like clipRect) I lose the bitmap date from the movie clip I am capturing from. Is there a fix for this? It works fine as long as I don't define all those parameters.
303dreams said on Feb 27, 2007 at 8:51 PM :
This is a fantastic new feature, mich needed for flash. Ive been trying to implement it for a movie to use it as a way to fade in a copy of a movie clip that would not usually be able to be faded under normal circumstances (i.e has components used, form fields ect) but it seems components such as scroll bars in the original movie clip do not get captured to the bitmap...

has anyone had any success with this, or know a way to do this?
fillcell Gerra said on May 14, 2007 at 2:20 PM :
I got a problem on merging transparent bitmaps, which seems a bit difficult to explain. Please take a look on it on our server, if you can. I've uploaded it with pictures, SWF, and some source.

http://www.fillcell.com/poster
dr deetee said on May 30, 2007 at 6:25 PM :
If you draw into a BitmapData with a MovieClip as the source, then the smoothing parameter has no effect. If you are scaling is involved, then it appears pixelated for whatever smoothing value you insert.

In my case, I worked around this issue by first drawing unscaled from my MovieClip into a temporary BitmapData, and then drawing that into my final BitmapData with scaling and smoothing.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/8/main/00001950.html