8 Replies Latest reply on Dec 4, 2009 7:16 AM by Scott R. Hamilton

    Loading an image from a known path

    Scott R. Hamilton Level 1

      In my application the user is prompted to select a .jpg image file from disk on the

      client machine which is then loaded  into the app's image control. This works fine.


      There is also a Print function which uses it's own <mx:Image> control into which

      the .jpeg selected by the user should be transferred.


      Since the image has already been successfully loaded into the previous screen,

      the image path is known and I was hoping passing the path as a string into the

      print routine and then simply



           formPrintView.imPrtPict.source = sImagePath;  


      would result in the picture being printed in the image control on the print form.


      But the image does not print. I thought I could perhaps load it anew but there

      doesn't seem to be a way in Flex to load using a path name without using the

      fileReference.browse() method.



        • 1. Re: Loading an image from a known path

          You can't access the path from FileReference and you can't load a file without displaying the browse dialog. Since you have already loaded the file and assigned the jpg to one Image control, why can't you assign the same data to a second Image control?

          • 2. Re: Loading an image from a known path
            Scott R. Hamilton Level 1

            I tried to do just that by passing the pathname returned by the browse method

            into the print routine and assigning it to the Image.source property in the print



            I tried both



               formPrintView.imPrtPict.source = sImagePath;  


            Neither attempt succeeded. I got no errors but the Image control  remains



            It looks like flex wants to reload the .jpeg file but I cannot expect the user to

            browse for the file a second time.



            • 3. Re: Loading an image from a known path
              paul.williams Level 4

              The browse method doesn't return a path. How are you setting the source on your first image? Can you post the code?

              • 4. Re: Loading an image from a known path
                Scott R. Hamilton Level 1

                Here's an abstracted version of the code where the picture file is browsed and



                <?xml version="1.0"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  backgroundGradientAlphas="[1.0, 1.0]"
                    backgroundGradientColors="[#F1C97C, #F1C97C]" width="1500" height="1134.0151">
                    <mx:Script source="PrintRecipe.as"/>


                            import mx.events.MenuEvent;
                            import mx.controls.Alert;
                            import mx.collections.*;
                            import mx.events.ItemClickEvent;
                            import mx.core.UIComponent;
                            import flash.external.*;    
                            import flash.net.FileReference;


                            public var sImagePath:String = "C:/Users/Public/Pictures/";


                            public var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png");
                            public var PictTypes:Array = new Array(imageTypes);
                            public var fileRef:FileReference = new FileReference();   
                            // Provides user with a file select dialog
                            //                     ________
                            private function BrowseFS(event:MouseEvent):void { 
                               fileRef.addEventListener(Event.SELECT, loadCompleteHandler);
                            // BrowseFS
                            // Handles the browse select event
                            //                     _________________
                            private function loadCompleteHandler(event:Event):void { 
                               //Alert.show("Image finished loading: " +sImagePath + fileRef.name)   
                               imPict.source = sImagePath + fileRef.name;
                               //imPict.load( fileRef.name )
                            // loadCompleteHandler


                    <mx:Panel title="My Title" height="1065.1515" width="1400"
                        paddingTop="10" paddingLeft="10" borderColor="#F4DCBD" backgroundColor="#F6F3E0">
                        <mx:Canvas width="1204" height="326">
                            <mx:Image x="833" y="50" width="188" height="145" id="imPict" source="NULL">
                            <mx:Label x="833" y="19" text="Picture" width="188" color="#3C3A0B" fontSize="14" fontWeight="bold" textAlign="center"/>
                            <mx:Button x="833" y="224" label="Add Picture" width="188" click="BrowseFS(event)"/>
                            <mx:Label x="769" y="203" text="Files must be in : Desktop/Public/Pictures" fontSize="8"/>




                The Image conrtrol is loaded at run time with sImagePath + fileRef.name. This works.


                I pass this through to the print function which tries to load the Image control in the print form with

                the same path. This does not work.



                • 5. Re: Loading an image from a known path
                  paul.williams Level 4

                  Is your second image on the displaylist?


                  Note that if you are planning to deploy your application on a web-server then the approach you are using won't work. If a SWF is loaded from a remote site it cannot read local files in this way. Your application is only working because you are running it from a local disk. See the link below for more:



                  • 6. Re: Loading an image from a known path
                    Scott R. Hamilton Level 1



                    thanks for your assistance...


                    I'm not sure I understand. The user would have a .jpeg on his local machine. She would select the

                    file via the fileReference.borwse dialog. Flex would load the file into the running program's

                    address space, save it to a database linked to the transaction being performed and optionally

                    print it which what I am trying to acomplish here.


                    Is the menthod I am trying to employ not the correct one?



                    • 8. Re: Loading an image from a known path
                      Scott R. Hamilton Level 1



                      thanks for the tips. I can see that there are many more issues involved here than

                      initially meet the eye.


                      For a quick print of the created data record object I have been passing the contents

                      of the text controls to a print routine (action script file) which populates the controls

                      on the print form.


                      If I actually succeed in creating a array object representing the image.jpeg I should

                      be able to pass a reference to it into the print routine so that it can be rendered on

                      the print form. Right?


                      Or should I first store it as some sort of global object visible to all routines in the+



                      Thanks again,