Skip navigation
Currently Being Moderated

My image is one size, the byte array is another?

Jul 31, 2012 12:12 PM

I have a very similar problem, but no solution:

I need to png encode an Image that is 130 x 30 but the byteArray it was created with is not long enough. I can see it right in front of me.


<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:mx=""
    width="160" height="114" horizontalAlign="center">


   import mx.core.Application;
   import mx.core.IFlexDisplayObject;
   import mx.managers.PopUpManager;
   private static var instance:Test = null;
   private var strng:String ="";
   private var png:ByteArray;
   private var result:ByteArray;
   public static function ready(str:String):void{
    if (instance == null) instance = new Test();
    instance.strng = str;;
   private function show() : void {
    result = stringToByteArray(strng);
    if (!this.isPopUp) {
     PopUpManager.addPopUp(this, Application.application  as DisplayObject, false);
   private static function setPopupHelpWindowStyle(ifdo:IFlexDisplayObject) : void {
    var isc:IStyleClient = IFlexDisplayObject(ifdo) as IStyleClient;
    isc.setStyle("modalTransparency", 1);
   private static function stringToByteArray(str:String) : ByteArray {
    var result:ByteArray = new ByteArray();
    var i:int = 0;
    while (i < str.length) {
     result.writeByte(Number("0x" + str.substr(i, 2)));
     i += 2;
    return result;
   public function encode():void{
    var coder:PNGEncoder = new PNGEncoder;
    instance.png = coder.encodeByteArray(instance.result, instance.img.width, instance.img.height);
<mx:Image id="img" x="10" y="10" width="130" height="30"/>
<mx:Button label="Button" click="encode()"/>


When I try to encode it I get:


Error: Error #2030: End of file was encountered.
at flash.utils::ByteArray/readUnsignedInt()
I get an error because bArray is created from a string that is only 3540 characters long, which is only 1720 bytes. I can see the image, and it fills the entire box, but I do not know what type of alogorthm is used to expand the byteArray to fill it.


Is there a method to access the pixels of the image similar to the BitmapData function, where I can iterate through the pixels and read the bits directly from the display to build a new ByteArray? Or a way to make it into a bitmapdata object?


var bmd:BitmapData = ???;
var useableByteArray:ByteArray = new ByteArray();

for (var i:int =0; i < img.width; i++){
for (var j:int = 0; j < img.height; j++){
  useableByteArray.writeByte(bmd.getPixel32(i, j));


Message was edited by: eboda_kcuf

  • Currently Being Moderated
    Jul 31, 2012 5:15 PM   in reply to the0bot

    Are you looking for BitmapData.draw()?  Also, a common problem is that the ByteArray’s position is not set back to 0 after writing to it so subsequent reads start from the end.

    Mark as:
  • Currently Being Moderated
    Aug 1, 2012 11:32 PM   in reply to the0bot

    mx:image usually takes a JPG, PNG or GIF so it would be fewer bytes than 4 * h * w.  The bitmapData will have 4 * h * w bytes.

    Mark as:
  • Currently Being Moderated
    Aug 2, 2012 9:01 AM   in reply to the0bot

    Then use BItmapData.draw() on the Image?

    Mark as:
  • Currently Being Moderated
    Aug 3, 2012 2:10 PM   in reply to the0bot

    Flash does have some idiosyncracies.  Images are loaded asynchronously since they could be coming over a very slow network, so maybe the first image isn’t ready the first time you call encodeImage().  Are you waiting for the right event before running the function?


    It looks like you have a legitimate JPEG byte array other wise img2 wouldn’t load it.  So, assuming that, is the missing piece how to write that out to a server?  See tella/ for how we write PNGs to a server.

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points