Skip to main content
Participating Frequently
August 11, 2009
Answered

Uploading an image dynamically into Flash

  • August 11, 2009
  • 1 reply
  • 1152 views

I'd upload an image choosen by the client, but I want to wrap it in a movieclip in order to manipulate its size and color with color transform. How can I do it? (AS3 in Flash 10)

My code:

import flash.display.SimpleButton;
   import flash.net.FileReference;
   import flash.net.FileFilter;
   import flash.events.IOErrorEvent;
   import flash.events.Event;
   import flash.utils.ByteArray;
   import flash.display.*;
   import flash.geom.*;

   var loadFileRef:FileReference;

   const FILE_TYPES:Array = [new FileFilter("Image Files", "*.jpg;*.jpeg;*.gif;*.png;*.JPG;*.JPEG;*.GIF;*.PNG")];

    function loadFile(event:MouseEvent):void {
    loadFileRef = new FileReference();
    loadFileRef.addEventListener(Event.SELECT, onFileSelect);
    loadFileRef.browse();
   }

    function onFileSelect(e:Event):void {
    loadFileRef.addEventListener(Event.COMPLETE, onFileLoadComplete);
    loadFileRef.load();
   }

    function onFileLoadComplete(e:Event):void {
       var loader:Loader = new Loader();
       loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onDataLoadComplete);
       loader.loadBytes(loadFileRef.data);
    loadFileRef = null;
   }

    function onDataLoadComplete(e:Event):void {
    var bitmapData:BitmapData = Bitmap(e.target.content).bitmapData;
   
    if((bitmapData.height)<=(bitmapData.width))
    {
                var r:Number = bitmapData.height/bitmapData.width;
                var ra:Number = 500*r;

                var matrix:Matrix = new Matrix();
                matrix.scale(500/bitmapData.width, ra/bitmapData.height);
               
    graphics.clear();
    graphics.lineStyle(1, 0x000000);
    graphics.beginBitmapFill(bitmapData, matrix, false);
    graphics.drawRect(0, 0, 500, ra);
    graphics.endFill();
   
    } else {
    
                var d:Number = bitmapData.width/bitmapData.height;
                var da:Number = 500*d;

                var matrix2:Matrix = new Matrix();
                matrix2.scale(500/bitmapData.width, da/bitmapData.height);
    
    graphics.clear();
    graphics.lineStyle(1, 0x000000);
    graphics.beginBitmapFill(bitmapData, matrix2, false);
    graphics.drawRect(0,50, 500, da);
    graphics.endFill();
    }
   }

  boton.addEventListener(MouseEvent.CLICK, loadFile);

Any help will be very appreciated. Madrid.

This topic has been closed for replies.
Correct answer kglad

you don't need to do anything with the movieclip class.  all displayobjects (including loaders) can be transformed using their transform property and all can be resized.

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
August 11, 2009

you don't need to do anything with the movieclip class.  all displayobjects (including loaders) can be transformed using their transform property and all can be resized.

bendayanAuthor
Participating Frequently
August 11, 2009

Thank you for yur reply.

1) I traied to move the rectangle to no avail. I cant aplly moveTo() if I don't have a named instance of the rectangle. That is why I need to enclose the rectangle in a movieclip or any other container (Flash, not Flex).

2) I tried to apply a color transformation and get nothing. You know, color transformatoin must be applied to an instance which I don't have. am I wrong? Please, tell me.

3) Also, I want to make the rectangle draggable (another issue).

Thank you for your help. Madrid

kglad
Community Expert
Community Expert
August 11, 2009

i thought you were trying to manipulate a loaded image.  what's a rectangle have to do with that?

how'd you try to apply a color transform?