Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a string on BitmapData

how can I draw strings onto BitmapData, is there something like Java´s Graphics.drawString()?

like image 523
Jarek Avatar asked Aug 08 '10 18:08

Jarek


2 Answers

In Actionscript, the most natural way of handling this, I think, would be using a container such as Sprite and drawing using it's graphics object and / or adding other display objects as children. Then you could take your "snapshot" when / if necessary, to get the pixel data.

For adding text, creating a TextField is the simplest option.

Anyway, you could write a little function that does this on an existing BitmapData, if you wanted. Here's a sketch of how such a function could be written:

function drawString(target:BitmapData,text:String,x:Number,y:Number):void {
    var tf:TextField = new TextField();
    tf.text = text; 
    var bmd:BitmapData = new BitmapData(tf.width,tf.height);
    bmd.draw(tf);
    var mat:Matrix = new Matrix();
    mat.translate(x,y);
    target.draw(bmd,mat);
    bmd.dispose();
}

// use
var bitmap:BitmapData = new BitmapData(400,400);
// let's draw something first (whatever is on the stage at this point)
bitmap.draw(stage);
drawString(bitmap,"testing",100,50);
// display the result...
addChild(new Bitmap(bitmap));
like image 65
Juan Pablo Califano Avatar answered Sep 18 '22 05:09

Juan Pablo Califano


You can draw a TextField into your bitmap :

import flash.text.TextField;
import flash.display.BitmapData;
import flash.display.Bitmap;

var tf:TextField=new TextField();
tf.text="Hello world";
var bd:BitmapData=new BitmapData(200,200, false,0x00ff00);
bd.draw(tf);
var bm:Bitmap=new Bitmap(bd);
addChild(bm);
like image 40
Patrick Avatar answered Sep 18 '22 05:09

Patrick