2 Replies Latest reply on May 11, 2009 7:13 AM by Starlover_jacob

    Help needed trying make thumbnails from large Image Files

    TOdd_23523523 Level 1
      I posted this to the Flex message bored, too. I didn't notice the Actionscript 3.0 until after. Sorry. Anyway, I have some ActionScript 3.0 code, created with Flex that will load a large JPEG image (say 3000x2000 pixels) that I'm trying to create a 100 pixel thumbnail. I have the code working where I generate the thumbnail, but it's not maintaining the aspect ratio of the original image. It's making it square, filling in white for the part that doesn't fit.
      I've tried just setting the height or width of the new image, but that doesnt render well, either.
      To see what I'm talking about, I made a screen shot, showing the before image, and the rendered as thumbnail image: Image of demo app at Flickr.

      Now, there's a few things important to note. I'm saving the thumbnail off as a JPEG. As you can see in my sample application, the original renders fine with the proper aspect ratio, it's when I'm copying the bytes off the bitmapdata object, where I need to specify a width and height, that the trouble starts. I've also tried using .contentHeight and .contentWidth and some division to manually specify a new bitmapdatasize, but these values seem to always have NaN.

      private function makeThumbnail():void{
      // create a thumbnail of 100x100 pixels of a large file
      // What I want to create is a a thumbnail with the longest size of the aspect
      // ratio to be 100 pixels.
      var img:Image = new Image();

      //Add this event listener because we cant copy the BitmapData from an
      /// image until it is loaded.
      img.addEventListener(FlexEvent.UPDATE_COMPLETE, imageLoaded);
      img.visible = true;

      // This is the image we want to make a thumbnail of.
      img.id = "testImage";
      this.addChildAt(img, 0);


      private function imageLoaded(event:Event):void
      // Grab the bitmap image from the Input Image and
      var bmd:BitmapData = getBitmapDataFromUIComponent(UIComponent(event.target));

      //Render the new thumbnail in the UI to see what it looks
      theImage.source = new Bitmap(bmd); //new Bitmap(bmd);

      public static function getBitmapDataFromUIComponent(component:UIComponent):BitmapData
      var bmd:BitmapData = new BitmapData(component.width,component.height );
      return bmd;