Page 1 of 1
Trying to display an image in a widget
Posted: Tue Mar 17, 2015 6:46 pm
by William Jamieson
Hello. Trying to do something very simple. I am trying to display an image in a widget. I used these scripts which were copy pasted strait from the Library. There are many ways to display an image, but out of the numerous ways that I tried, this script seemed to error the least. Any idea on how to make this work?
Code: Select all
widget community.livecode.jamieson.ShowImage
use com.livecode.canvas
use com.livecode.widget
use com.livecode.engine
metadata title is "ShowImage"
metadata author is "William Jamieson"
metadata version is "1.0.0"
public handler OnPaint()
variable tData
put the contents of file "images/Acknowledgement Black 256.png" into tData
variable tImage
put image from data tData into tImage
end handler
end widget
This got me to an error stating that I do not have access to the file images/Acknowledgement Black 256.png even though the folder images is right next to the LCB file with the png in it.
Please let me know if anyone else has got images to appear in their widgets and how you did it! Thank you!
-Will
Re: Trying to display an image in a widget
Posted: Wed Mar 18, 2015 9:37 am
by LCMark
@WillieamJamieson: You're going in the right direction. The reason you are getting the error is because the current implementation of 'contents of file' will resolve a filename exactly as it would if you used 'open file' in LiveCode Script. i.e. Relative paths will be resolved relative to the 'folder'; absolute paths will be taken verbatim.
If you want to manipulate images from files which might come from 'outside' (e.g. a property a user sets on your widget), then you should use the 'image from file <...>'. This will resolve the filename in the same way as 'filename' properties do for other controls in LiveCode. If it is an absolute path then it will resolve it only using that. If it is relative then it will first try relative to the filename of the stack the widget is on, and then try relative to the current working directory.
The other kind of image you might want to manipulate will be ones which are included as private resources within the widget's package - this uses the 'image from resource file <...>' syntax. Here, the filename must be relative and is resolved relative to the resources folder which is included in with widget's package. Each package has a private set of resources which only the code within the package can access. This is probably closer to what you are trying to do here - the only rub is that this particular feature is still a WIP and so mileage may vary with getting it to work.
So, the reason your code throws the error is because it cannot find the file. If you put the stack you are testing the widget with next to the images folder then you should hopefully see something working.
By the way, I strongly suggest getting out of the habit of creating images and any other 'more hefty' resources in OnPaint. OnPaint can be called at any time and for all kinds of reasons and needs to be (as far as is possibly) entirely devoted to just rendering - i.e. you should precompute, and preload as much as possible when properties change so that OnPaint can be as fast as possible. Thus I'd suggest using a module-local variable to store your image, and initialize it in OnCreate or OnOpen instead.