Posted By

GunnarHKarlsson on 09/21/11


Tagged

scale Flex bitmapData


Versions (?)

scaleBitmapData


 / Published in: ActionScript 3
 

  1. private function scaleBitmapData(_bmd:BitmapData,
  2. targetContainerWidth:Number, targetContainerHeight:Number=0):Bitmap {
  3. //set matrix sx & sy
  4. var sx:Number = targetContainerWidth / _bmd.width;
  5. var sy:Number = 0;
  6.  
  7. if(targetContainerHeight==0){
  8. sy = sx;
  9. targetContainerHeight = _bmd.height * sy;
  10.  
  11. }else{
  12. sy = targetContainerHeight / _bmd.height;
  13. }
  14.  
  15. //instantiate new matrix, and set scaling
  16. var m:Matrix = new Matrix();
  17. m.scale(sx, sy);
  18.  
  19. //create new bitmapdata
  20. var newBmd:BitmapData = new BitmapData(targetContainerWidth, targetContainerHeight);
  21. //draw new bitmapdata with matrix
  22. newBmd.draw(_bmd, m);
  23. //create final bitmap with new bitmapdata
  24. var newBmp:Bitmap = new Bitmap(newBmd);
  25. //set smooting to true
  26. newBmp.smoothing = true;
  27.  
  28. return newBmp;
  29. }
  30.  
  31. private function scalePhotoToTargetWidth(image:Bitmap,targetContainerWidth:int):Bitmap{
  32.  
  33. var scaleFactor:Number = targetContainerWidth / image.width;
  34. var targetContainerHeight:Number = scaleFactor * image.height;
  35.  
  36. var scaleParams:Matrix = new Matrix();
  37. scaleParams.scale(scaleFactor,scaleFactor);
  38.  
  39. var newBmd:BitmapData = new BitmapData(targetContainerWidth, targetContainerHeight);
  40. //draw new bitmapdata with matrix
  41. newBmd.draw(image.bitmapData, scaleParams);
  42. //create final bitmap with new bitmapdata
  43. var newBmp:Bitmap = new Bitmap(newBmd);
  44. //set smooting to true
  45. newBmp.smoothing = true;
  46.  
  47. return newBmp;
  48. }
  49.  
  50. private function scalePhotoToTargetHeight(image:Bitmap, targetContainerHeight:int):Bitmap
  51. {
  52. var scaleFactor:Number = targetContainerHeight / image.height;
  53. var targetContainerWidth:Number = scaleFactor * image.width;
  54.  
  55. var scaleParams:Matrix = new Matrix();
  56. scaleParams.scale(scaleFactor,scaleFactor);
  57.  
  58. var newBmd:BitmapData = new BitmapData(targetContainerWidth, targetContainerHeight);
  59. //draw new bitmapdata with matrix
  60. newBmd.draw(image.bitmapData, scaleParams);
  61. //create final bitmap with new bitmapdata
  62. var newBmp:Bitmap = new Bitmap(newBmd);
  63. //set smooting to true
  64. newBmp.smoothing = true;
  65.  
  66. return newBmp;
  67. }

Report this snippet  

You need to login to post a comment.