Skip navigation
Currently Being Moderated

How to resize original image that received from server

Jul 24, 2012 10:48 PM

Tags: #actionscript_3.0 #flex4.5

Hi Helper's,

 

I'm getting error on time of resizing image such as "Error #2030: End of file was encountered". I want to resize as per specified size. Code is written in Flash builder 4.5. Please find my code below and help me to correct.

 

 

<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

               xmlns:s="library://ns.adobe.com/flex/spark"

               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"

               initialize="initNew()" xmlns:local="*">

    <fx:Declarations>

        <!-- Place non-visual elements (e.g., services, value objects) here -->

    </fx:Declarations>

 

    <fx:Script>

        <![CDATA[

            import mx.controls.Alert;

            import mx.graphics.codec.JPEGEncoder;

 

            private const image_path:String = "sample.jpeg";

            private var loader:Loader;

            private var request:URLRequest; 

 

            private var loadBD:BitmapData;

            private var cropBD:BitmapData; 

 

            private var squareW: int = 75;

            private var squareH: int = 100;

 

            private function initNew():void

            {

                loader=new Loader();

                request=new URLRequest(image_path);

                loader.contentLoaderInfo.addEventListener(

                    Event.COMPLETE,onComplete);

                loader.load(request);

            }

 

            private function onComplete(event:Event):void {

                try

                {

                    loadBD = Bitmap(event.currentTarget.content).bitmapData;

 

                    var bitmapData:BitmapData = new BitmapData(squareW, squareH, true, 0);

                    bitmapData.draw(loadBD);

//Getting error in this line

                    var ba:ByteArray = (new JPEGEncoder()).encodeByteArray(bitmapData.getPixels(new Rectangle(0, 0, squareH, squareH)), squareH, squareH, true);

                    ba.position = 0;

                    imgResized.source = ba;

                }

                catch(e:Error)

                {

                    Alert.show(e.message);

                }

            }

 

        ]]>

    </fx:Script>

    <s:BorderContainer id="bcImage" width="75" height="100" maxWidth="59" borderWeight="1" borderColor="red">

        <s:Image id="imgResized" width="100%" height="100%"/>

    </s:BorderContainer>

 

</s:Application>

If anyone faced similar kind of problem?

 
Replies
  • Currently Being Moderated
    Jul 25, 2012 4:34 PM   in reply to grace-glory

    Is there a specific reason why using a BitmapImage would not work for your use case? If you are worried about the overhead, I would take a look at the source and copy out the bits you need. One thing I noticed after looking at the source is that they do not use any kind of encoder to perform that functionality. Is there a specific reason you need this?

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 29, 2012 11:57 PM   in reply to grace-glory

    Please try replacing the Image declaration line with the one as follows:

     

    <s:Image  id="imgResized" width="100%" height="100%" scaleMode="stretch" maintainProjectionCenter="false"/>

     

    It might be helpful to resize the original image. And let me know if it works or not!

     
    |
    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