base64Encode() of the kernel class ‘Image’ does return an empty string

A partner asked me why the base64Encode method of the Image class does always return an empty string. In the following example:

    1:  Image image;
    2:  str sBase64Encode;
    3:  ;
    4:  image = new Image();
    5:  image.loadImage(@'C:\sample.bmp');
    6:   
    7:  sBase64Encode = image.base64Encode();
    8:  info(sBase64Encode);

will display an empty string for the variable sBase64Encode in the InfoLog window.

This is because the base64Encode method is inherited from the BinData class and encodes the file object in the Image class and not the image object which is loaded with the loadImage method. Unfortunately the file object is not initialized implicitly when loading a file, so that you have to call the method loadFile which initializes the file object. Now you have to initialize the image object and this can be done by creating an image object with the method createImage:

    1:  Image image;
    2:  str sBase64Encode;
    3:  ;
    4:  image = new Image();
    5:  image.loadFile(@'C:\sample.bmp');
    6:   image.createImage(10,10,10);     //sample values 
    7:   
    8:  sBase64Encode = image.base64Encode();
    9:  info(sBase64Encode);

or by loading the image file:

    1:  Image image;
    2:  str sBase64Encode;
    3:  ;
    4:  image = new Image();
    5:  image.loadImage(@'C:\sample.bmp');
    6:    image.loadFile(@'C:\sample.bmp');  
    7:   
    8:  sBase64Encode = image.base64Encode();
    9:  info(sBase64Encode);

The same behavior is true for the class ImageList if you load resources from a dll (for example shell32.dll) with the loadIcons-method into an ImageList-class. In that case it is unfortunately not possible to encode the images. You have to extract the icons and load each icon separately instead.