4 Replies Latest reply on Jul 30, 2012 7:38 AM by grace-glory

    How to resize original image that received from server

    grace-glory

      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?

        • 1. Re: How to resize original image that received from server
          drkstr_1 Level 4

          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?

          • 2. Re: How to resize original image that received from server
            grace-glory Level 1

            drkstr_1 wrote:

             

            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?

            Thanks for your reply and happy to see you.

             

            No reason, I got above code When I google it and changed accordinaly to meet req. My requirement is, server will be giving 400*350 (Byte array) size image. That one what to resize into 75*100 and apply it to image component. I'm fine with any approarch, however if the image size is less than 75*100, then should not stretch the image. I have tried with fillMode (BitmapFillMode.CLIP), scaleMode(BitmapScaleMode.LETTERBOX). Please update your concern on same.

             

            Note: Plz ignore this maxWidth="59" property in BorderContainer property.

            • 3. Re: How to resize original image that received from server
              komal harkut Level 1

              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!

              • 4. Re: How to resize original image that received from server
                grace-glory Level 1

                Thanks for reply. I dont want to stretch the image. if it's image size is less than specified, then it will be streching and no quality.