Using ICODecoder Class to Read ICO file and Display it as Bitmap in ActionScript

applications If you are an experienced Java development, you might know the ICODecoder class ( package com.sun.jimi.core.decoder.ico ). Using this class, We can read ICO file in Java. But how can we do the same in actionScript? The following ICODecoder Class is capable of reading the ICO format in actionscript. Usage is very simple:

function loadICOFile( url:String ):void {
  var loader:URLLoader = new URLLoader();
  loader.dataFormat = URLLoaderDataFormat.BINARY;
  loader.addEventListener( Event.COMPLETE, onCompleteLoad );
  loader.load( new URLRequest( url ) );
}
function onCompleteLoad( e:Event ):void {
  var loader:URLLoader = e.target as URLLoader;
  var decoder:ICODecoder = new ICODecoder();
  var arr:Array= decoder.decode( loader.data );

  for ( var i:int = 0; i < arr.length; i++ ) {
    var data:ICOImageData = arr[i] as ICOImageData;
    if ( ( data.info.width == 16 ) && ( data.info.height == 16 ) ) {
      // if 16×16 bit
      addChild( new Bitmap( data.image ) );
    }
  }
}

The following is full source code of ICODecoder class :

package com.voidelement.images.ico {
  import flash.display.BitmapData;
  import flash.errors.IOError;
  import flash.utils.ByteArray;
  import flash.utils.Endian;

  public class ICODecoder {
    private static var _verbose:Boolean = false;
    private static function get verbose():Boolean { return _verbose; }
    private static function set verbose( value:Boolean ):void { _verbose = value; }

    private var _header:ICOFileHeader;
    public function get header():ICOFileHeader { return _header; }

    /**
     *  Constructor
     */
    public function ICODecoder() {

    }

    /**
     * decode
     *
     * @param byte data of ICO file
     */
    public function decode( stream:ByteArray ):Array {
      _header = new ICOFileHeader( stream );

      var info_arr:Array = new Array();
      for ( var i:int = 0; i < header.num; ++i ) {
        info_arr.push( new ICOInfoHeader( stream ) );
      }

      var image_arr:Array = new Array();
      for ( var j:int = 0; j < header.num; ++j ) {
        image_arr.push( new ICOImageData( stream ) );
      }

      return image_arr;
    }

    /**
     * print log info
     */
    public static function log( message:String ):void {
      if ( verbose ) {
        trace( message );
      }
    }
  }
}

For more details, please goto to: http://www.libspark.org/svn/as3/ICODecoder/src/com/voidelement/images/ico/

Enjoy!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

3 Responses to “Using ICODecoder Class to Read ICO file and Display it as Bitmap in ActionScript”

  1. Paul Hayes says:

    You haven’t included the ICOFileHeader and ICOImageData class, which seem to do the most import tasks! Any chance you could post up those files.

    thanks for you time,

    Paul

  2. Kyle says:

    This is not working properly..
    the mask is not correct.

    And its not showing all the icon sizes.. i get errors for 48×48 etc.

    Is there a fix for this?

Leave a Reply