This program gives a strange cast error when run in the IDE but not when optimised compiled. The much bigger program that this was cut down from also ran fine when compiled.
Thanks agraham.
This bug will be fixed in the next version (one after 6.50).
It will happen when the assigned image is a reference to an external object.
The workaround is to reassign the image before changing the mode property: