Page 1 of 1

How do I import a image file

Posted: Thu May 16, 2013 2:39 pm
by tanjc
Hi,

I have some livecode experts could help me with the following problem.

I am trying to import images using the following script, the result display garbage.
I believed the problem lies in the field "text". How can I overcome this problem.

on mouseUp
answer file "Select a file" with filter "Web Graphics,*.jpg;*.gif;*.png"
if it <> "" then
put it into theFilePath
--don't forgett the brackets!
put URL ("file:" & theFilePath) into field "text"
else
--no file was selected, or cancel was pressed
beep
end if
end mouseUp

Thanks for your help.

Re: How do I import a image file

Posted: Thu May 16, 2013 3:05 pm
by Klaus
Hi tanjc,

well, FIELDS are supposed to display TEXT and IMAGES to display images :-)

You CAN of course display in a field, but not this way, it will requre that the
image is already IN the stack.

Do this to import the user selected image into the stack:

Code: Select all

on mouseUp
  answer file "Select a file" with filter "Web Graphics,*.jpg;*.gif;*.png"
  put it into theFilePath
  
  ## I always check for the NEGATIVE side of things,
  ## so I can avoid long and maybe nested IF...THEN clauses :-)
  if theFilePath = EMPTY then
    beep
    exit mouseup
  end if
  
  ## Import image into stack as a new image:
  import paint from file theFilePath
  ## Done :-)
end mouseUp
Best

Klaus

Re: How do I import a image file

Posted: Thu May 16, 2013 3:06 pm
by Dixie
Hi...

try something like this...

Code: Select all

on mouseUp
   answer file ""
   if it is not empty then set the filename of image 1 to it
end mouseUp
Notice the difference, with 'set the filename'

Dixie

Re: How do I import a image file

Posted: Sun May 19, 2013 7:32 am
by tanjc
Thanks very much to Klaus and Dixie. The codes you provide are very useful, exactly what I am looking for.
I am new to LiveCode and have a lot to learn from you guys.