In this day and age.. we come across as images everyday – whether we are searching the net, reading news or books or using applications.
I am only discussing Silverlight part of WP7 development here. My knowledge of XNA is too little to do anything useful with it… let alone blog about it.
Lets open a Windows Phone Project. Most people tend to store their content in specific folders e.g. all pictures and images go in Images folder etc. Lets create Images folder and import our sample image.
Right click Images folder and click Add >> Existing Item
Browse to the correct location and find the file. Click okay and the file is imported in the project.
Lets look at the properties of the imported file. Default build action is set to Resource. Lets now add Image control and display the image.
<Image Source=”/PhoneApp1;component/Images/IMG_5710.jpg” />
As you can see its a slightly complicated path though there’s nothing wrong with it. It works just fine. Now lets try setting the image BuildAction to Content. When setting it to content, please remember to set “Copy to Output Folder to “Copy Always”. Now we need to modify the XAML
<Image Source=”/Images/IMG_5710.jpg” />
Please ensure that Copy to Output is set and that you use /Images to denote Images directory.
If we run the app, it runs exactly as it did before.
Now lets look at the contents of the XAP file. XAP file is just a zipped file containing various assemblies and resources.
|Contents of XAP|
|Contents of Assembly|
the screenshots make the difference obvious. When setting the picture as resource, it is included in the Assembly. When setting it as Content, it is included in the XAP but not in assembly. That makes all the difference. Imagine you had a number of pictures – if you chose them to be Resource, they would be loaded the same time as CLR loading your assembly. If each image was say 500k and you had 10 that would make your assembly 5 megs larger and slower to load. However if you set them to be Content, they are still there in the package just not in the Assembly and they are loaded as required.
The bottom line is: Unless you have a very good reason, set images, videos, files etc to build action - Content and “Copy to Output Folder to “Copy Always”.