In a previous example, “Using the ImageSnapshot class to capture images as JPEGs or PNGs in Flex 3″, we saw how you could convert an item on the display list into a base64 encoded string.
The following example shows how you can take a base64 encoded string and load it into a Flex Image control using the decode() and toByteArray() methods in the mx.utils.Base64Decoder class, and the load() method in the mx.controls.Image class.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/17/displaying-an-image-saved-as-a-base64-encoded-string-in-flex-3/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white"
creationComplete="init();">
<mx:Script>
<![CDATA[
import mx.utils.Base64Decoder;
private var base64Dec:Base64Decoder;
private function init():void {
var byteArr:ByteArray;
base64Dec = new Base64Decoder();
base64Dec.decode(logo);
byteArr = base64Dec.toByteArray();
img.load(byteArr);
}
]]>
</mx:Script>
<mx:String id="logo" source="logo.txt" />
<mx:Form width="100%" height="100%">
<mx:FormItem label="image:">
<mx:Image id="img" />
</mx:FormItem>
<mx:FormItem label="source:"
width="100%"
height="100%">
<mx:TextArea id="textArea"
text="{logo}"
editable="false"
width="100%"
height="100%" />
</mx:FormItem>
</mx:Form>
</mx:Application>
/src/logo.txt
/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2MBERISGBUYLxoaL2NCOEJjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY//AABEIADIAMgMBEQACEQEDEQH/xAGiAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgsQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqS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++TQB7Zag/ZYeP4F/lQA9o0LElFJ9xQAvlR/880/75FAB5Uf/PNP++RQAeVH/wA80/75FAB5Uf8AzzT/AL5FMA8qP/nmn/fIoAcBxSA//9k=
View source is enabled in the following example.





You are Simply great!!!!
This code is not working, there is no class “Base64Decoder” in mx.utils package :(
Tony,
Are you using Flex 3? I believe I compiled this using Flex 3.0.0.477 (the retail Flex 3 SDK)
It is in Flex 3: http://livedocs.adobe.com/flex/3/langref/mx/utils/Base64Decoder.html
But not Flex 2: http://livedocs.adobe.com/flex/201/langref/mx/utils/package-detail.html
Peter
thanks! it works with flex 3!! it took me hours to find out this solution
this post should put into the flex api document
base64Eec = new Base64Eecoder();
There shows one more error 4 me like thaat:call to a possibly undefined method Base64Eecoder
my code is…..
public function postData():void { var request:HTTPService = new HTTPService(); var base64Eec:Base64Encoder; request.url = "http://twitter.com/statuses/update.xml"; request.method = "POST"; request.addEventListener(ResultEvent.RESULT, handleComplete); var username:String = "username"; var password:String = "password"; base64Eec = new Base64Eecoder(); base64Eec.encode(username + ":" + password); var encodedCredentials:String = base64Eec.toString(); var headerArray = new Array( new URLRequestHeader("Authentication", "Basic " + encodedCredentials));can u help me please…….
anoop,
Are you using Flex 3? This class was added in Flex 3, and isn’t available in Flex 2 projects.
Also, make sure you’ve imported the Base64Encoder/Base64Decoder class as well:
Peter
Thank you, this is indeed helpful.
First things first - superb blog - would be lost without it. Thank you.
I’m working on some code at the moment and need to display a Base64 String as an image. Used the example above and it worked first time. Thank you.
*BUT* (isn’t there always a but…)
What to do when the String is coming from a database and sometimes contains invalid data.
I thought it would be a simple matter to trap the error and display an alternative image.
Nothing seems to work.
I’ve tried try/catch blocks. I’ve tried using ioError event on the image component.
I’m going out of my mind - Could you suggest a way to trap invalid data. If I can trap the error I can deal with it. Nothing seems to work.
Kind Regards
Once again - the blog is superb :)
Rich
Thanks for the info on this! It saved my butt.
I had been trying to figure out how to create Power Point presentations from Flex and ColdFusion, and this was the trick.
By encoding the images, I am able to embed the images into a .MHT file (which is basically xml), and Power Point can read this file and display the images, just like a ppt file. I even named the file with a .ppt extension so Power Point is the default app to open it, and its all working great.
cheers!
Dustin
thanks this worked. However I am receiving an image as a byte array in a string not base64hex in a string. Data in string looks like this…34,645,234,653,122,12,32,43,543,…
Ho do i convert this string of numbers to a bytearray and in turn into an image
By the way this is a magnificent site, well done.
thanks,
Ken
ken larkin,
I don’t know if this would work, but you could try converting the list of numbers into an array using the
split()method, then loop over the array of numbers and add them to a ByteArray using thewriteInt()method. Something like this:private function stringToByteArray(value:String):ByteArray { var arr:Array = value.split(","); var idx:uint; var len:uint = arr.length; var byteArr:ByteArray = new ByteArray(); for (idx=0; idx<len; idx++) { byteArr.writeInt(arr[idx]); } return byteArr; }But I’m sure there may be a few missing pieces. You could try asking on the FlexCoders mailing list, I’m sure somebody there has attemped what you’re trying to do.
Peter