2009-06-08 8 views

Répondre

67

Cela fonctionne:

var scale:Number = 1.0/6.0; 
var matrix:Matrix = new Matrix(); 
matrix.scale(scale, scale); 

var smallBMD:BitmapData = new BitmapData(bigBMD.width * scale, bigBMD.height * scale, true, 0x000000); 
smallBMD.draw(bigBMD, matrix, null, null, null, true); 

var bitmap:Bitmap = new Bitmap(smallBMD, PixelSnapping.NEVER, true); 
+0

fonctionne parfaitement, merci! –

+2

this '0x000000' est très important, sans cela il n'y a pas de transparence – Cherniv

1

Sans écrire le code moi-même. La façon dont j'aborderais cela serait de créer un nouvel objet BitmapData de la taille désirée, puis d'utiliser la méthode bitmap.draw pour copier le grand dans le petit. La méthode bitmap.draw accepte également un argument matriciel que vous pouvez utiliser pour mettre à l'échelle lorsque vous copiez.

19
public function drawScaled(obj:IBitmapDrawable, thumbWidth:Number, thumbHeight:Number):Bitmap { 
    var m:Matrix = new Matrix(); 
    m.scale(WIDTH/obj.width, HEIGHT/obj.height); 
    var bmp:BitmapData = new BitmapData(thumbWidth, thumbHeight, false); 
    bmp.draw(obj, m); 
    return new Bitmap(bmp); 
} 

IBitmapDrawable est une interface pour DisplayObject et BitmapData.

de: http://www.nightdrops.com/2009/02/quick-reference-drawing-a-scaled-object-in-actionscript/

+0

Pas vraiment ce que je voulais, parce que je commençais avec un bitmapdata plutôt qu'un objet d'affichage. Merci quand même! – Iain

+1

facilement fixé en substituant DisplayObject avec BitmapData ;-) – Carlo

+0

celui-ci a fonctionné pour moi :-) – dharm0us

0

Le problème avec l'utilisation de la matrice mise à l'échelle est qu'il ne fait aucun anticrénelage ou lissage - c'est probablement OK si vous êtes sûr que vous ne jamais été réduction d'échelle, mais une méthode plus générale utiliserait la classe Image pour effectuer le redimensionnement. Dans AS3, cela ne serait jamais ajouté à la liste d'affichage, donc serait simplement utilisé "hors écran". Quelque chose comme ça (avec vos données bitmap comme « sourceBitmapData »):

var image:Image = new Image(); 
image.load(new Bitmap(sourceBitmapData, PixelSnapping.NEVER, true)); 

var scale:uint = 100/600; // this is from your example of 600x600 => 100x100 
var scaledWidth:uint = sourceBitmapData.width * scale; 
var scaledHeight:uint = sourceBitmapData.height * scale; 

image.content.width = scaledWidth; 
image.content.height = scaledHeight; 

var scaledBitmapData:BitmapData = new BitmapData(scaledWidth, scaledHeight); 
scaledBitmapData.draw(image.content); 

image = null; 

Vous pouvez ensuite utiliser « scaledBitmapData » à la place de « sourceBitmapData » à faire tout avec.

+0

D'où vient cette classe Image? http://help.adobe.com/fr_FR/FlashPlatform/reference/actionscript/3/index.html?filter_flashplayer=10.1 ne répertorie pas une classe Image comme faisant partie de la bibliothèque AS3. – Dwayne

+0

Voté comme aucune référence à la classe d'image – Jotham

+0

, il est soit http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/Image.html ou http://help.adobe.com /fr_FR/FlashPlatform/reference/actionscript/3/spark/components/Image.html –

10

Avec lissage:

function BitmapScaled(source:IBitmapDrawable, thumbWidth:Number, thumbHeight:Number):BitmapData { 
    var mat:Matrix = new Matrix(); 
    mat.scale(thumbWidth/source.width, thumbHeight/source.height); 
    var bmpd_draw:BitmapData = new BitmapData(thumbWidth, thumbHeight, false); 
    bmpd_draw.draw(source, mat, null, null, null, true); 
    return bmpd_draw; 
} 

Le procédé d'étirage accepte IBitmapDrawable qui est une interface pour DisplayObject et BitmapData.

0

Voici une variante de ce qui précède qui ajoute la prise en charge du zoom, de l'étirement et de la boîte aux lettres. Il peut ne pas fournir le support de découpage.

var newSizeBitmapData:BitmapData = resizeBitmapData(myBitmapData, newWidth, newHeight); 


/** 
* Resize display object or bitmap data to a new size 
**/ 
public static function resizeBitmapData(bitmapDrawable:IBitmapDrawable, width:Number, height:Number, scaleMode:String="none", 
             smooth:Boolean = true, transparent:Boolean = true, fillColor:Number = 0x00000000):BitmapData { 
    var sizedBitmapData:BitmapData; 
    var matrix:Matrix; 
    matrix = getSizeByScaleMode(width, height, Object(bitmapDrawable).width, Object(bitmapDrawable).height, scaleMode); 
    sizedBitmapData = new BitmapData(width, height, transparent, fillColor); 
    sizedBitmapData.draw(bitmapDrawable, matrix, null, null, null, smooth); 

    return sizedBitmapData; 
} 

// Get correct scale. Inspired from code in Apache Flex (license Apache 2.0) 
public static function getSizeByScaleMode(maxWidth:int, maxHeight:int, 
              width:int, height:int, 
              scaleMode:String="letterbox", 
              dpi:Number=NaN):Matrix { 

    var aspectRatio:String = (maxWidth < maxHeight) ? "portrait" : "landscape"; 
    var orientation:String = aspectRatio; 

    var matrix:Matrix = new Matrix(); 

    var scaleX:Number = 1; 
    var scaleY:Number = 1; 

    switch(scaleMode) { 
     case "zoom": 
      scaleX = Math.max(maxWidth/width, maxHeight/height); 
      scaleY = scaleX; 
      break; 

     case "letterbox": 
      scaleX = Math.min(maxWidth/width, maxHeight/height); 
      scaleY = scaleX; 
      break; 

     case "stretch": 
      scaleX = maxWidth/width; 
      scaleY = maxHeight/height; 
      break; 
    } 

    if (scaleX != 1 || scaleY != 0) { 
     width *= scaleX; 
     height *= scaleY; 
     matrix.scale(scaleX, scaleY); 
    } 

    matrix.translate(-width/2, -height/2); 

    matrix.translate(maxWidth/2, maxHeight/2); 

    return matrix; 
}