WP7 working with Images: Content vs Resource build action
published on: 2/2/2011 | Views: N/A | Tags: Images Silverlight
by WindowsPhoneGeek
Generally there are two ways to include an image(other resource) in a Windows Phone 7 project : as build action "Content" or build action "Resource". In this tutorial I will give some examples of how to reference images depending on their build action and will share some performance tips as well.
To begin with lets first say that in this demo I will use the following structure of the images:

Performance Consideration
For a better performance we would suggest that you compile your images with a "Build Action" of "Content" instead of the default "Resource". When adding new images to your project by default the "Build Action" is set to "Resource" (under the Properties window). Make sure to always change this to "Content" in order to reduce the size of your DLL, speeding up both app load and image load.
Content
If you set the build action to "Content" the image is included in the XAP alongside the DLL.
When you use "Build Action" of type "Content" then you access your image in this way:
XAML:
<Image Stretch="None" Source="/images/appbar.cancel.rest.png"/>
C#:
//Content
Uri uri = new Uri("/images/appbar.cancel.rest.png", UriKind.Relative);
BitmapImage imgSource = new BitmapImage(uri);
this.image.Source = imgSource;
Resource
All resources are embedded in your assembly (DLL).
When you use "Build Action" of type "Resource" then you access your image in this way:
XAML:
<Image Source="/WP7SampleProject3;component/images/appbar.feature.email.rest.png"/>
C#:
//Resource
Uri uriR = new Uri("/WP7SampleProject3;component/images/appbar.feature.email.rest.png", UriKind.Relative);
BitmapImage imgSourceR = new BitmapImage(uriR);
this.imageR.Source = imgSourceR;
Relative or Absolute URI?
Relative URI
Relative URIs specify the location of a resource file relative to the application root or the referencing XAML.
Absolute Uri
Absolute URIs specify the exact location of a resource file, and bypass the fallback mechanism.
RelativeOrAbsolute
If you are not sure whether of these two options to use you can waylays use the RelativeOrAbsolute.
For any reference you can take a look at the MSDN documentation.
You can get the full source code here.
You can also follow us on Twitter @winphonegeek
Comments
Thank u
posted by: kaushal on 7/27/2011 12:59:46 PM
Thanx a lot....
posted by: Eric on 8/8/2011 10:16:36 AM
Hi,
Thanks for all your posts.
Is it the same way with wav files ?
RE:Is it the same way with wav file
posted by: winphonegeek on 8/19/2011 6:40:42 PM
Yes the same considerations are valid for any file type as well.
Thanks
posted by: IFERDEN on 12/28/2011 4:00:54 PM
finally!!! thank you
Thanks
posted by: Mangesh on 2/28/2012 3:23:08 PM
Really Helpful, Thanks a lot....!
Content question
posted by: Gilles on 3/6/2012 3:35:01 PM
I have a little question... If you use 'Content', is there any way you can use these content-image/movies/etc.. via xaml. In your code-examples, the xaml-way of using 'content' is empty.
Thanks in advance!
Gilles
posted by: Davy on 3/30/2012 10:15:54 AM
I've been reading this everywhere, that "Content" should be faster than "Resource". I had performance problems with my application: when the loading screen dissapeared, you could clearly see the images being loaded (as fill brushes of rectangles in a canvas). It took about 1 second for the screen to be constructed. I solved this by simply setting the content type to "Resource" !!! Now you cannot see the canvas being built. Its just there immediately.
So "Content" is DEFINITELY not always faster!
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
