4 out of 6 rated this helpful - Rate this topic

Image.Source Property

Gets or sets the source for the image.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)
public ImageSource Source { get; set; }
<Image Source="uri"/>

XAML Values

uri

The string provided for the Source property is interpreted as a Uniform Resource Identifier (URI). This is in contrast to an Internationalized Resource Identifier (IRI). This distinction means that characters used to identify a Source that are outside of US-ASCII will need to be encoded.

Relative references are permitted. The relative reference is relative to the XAP file for the application rather than the HTML page that hosts the plug-in.

Cross-domain URIs are permitted, and specifying the scheme is permitted, but mixing URI schemes (such as accessing an HTTPS image from a Silverlight plug-in hosted on an HTTP-served HTML page) is not permitted.

The format-specific filename extensions are not required to be in the URI naming, but if the retrieved file is not determined to be a valid image format, a runtime exception is thrown.

Property Value

Type: System.Windows.Media.ImageSource
A source object for the drawn image.

Dependency property identifier field: SourceProperty

Note Note:

Silverlight does not support all image formats. See BitmapImage for information on the types of image sources and formats that can be used for an Image.

You can set the Source by specifying an absolute URL (e.g. http://contoso.com/myPicture.jpg) or specify a URL relative to the XAP file of your application.

In the JavaScript API, the equivalent Source property was set by a string that evaluated as a URI. In the managed API, this property uses an underlying ImageSource instance (actually this is usually a BitmapImage, which is a derived class).

You can set this property in XAML, but in this case you are setting the property as a URI. The XAML behavior relies on underlying type conversion that processes the string as a URI, and calls the BitmapImage(Uri) constructor. This in turn potentially requests a stream from that URI and returns the image source object.

See BitmapImage for information on the types of image sources and formats that can be used for an Image.

The ImageFailed event can occur if the initial Source attribute value in XAML does not specify a valid source.

The following example shows how to create an image.

<Image Source="myPicture.png" />
Image myImage = new Image();
myImage.Source = new BitmapImage(new Uri("myPicture.jpg", UriKind.RelativeOrAbsolute));
LayoutRoot.Children.Add(myImage);

In this example, the Source property is used to specify the location of the image you want to display. You can set the Source by specifying an absolute URL (e.g. http://contoso.com/myPicture.jpg) or specify a URL relative to the XAP file of your application. So for the previous example, you would need to have the XAP file in the same folder as myPicture.png.

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Set Source from referenced assembly
I think it would be useful to mention here that you can also set the source to a resource file from a referenced assembly.

Uri uri = new Uri("/ReferencedAssemblyName;component/Images/Image1.png", UriKind.Relative);

Image i = new Image() { Source = new BitmapImage(uri) };<Image Source="/ReferencedAssemblyName;component/Images/Image1.png" Width="15" Height="13"/>

Where "ReferencedAssemblyName" is the assembly name (no .dll),  and Image1.png is a resource in an Images folder, directly under the root of the "ReferencedAssemblyName" project.   There is no "component" folder -- that is just part of the syntax.
Anne
I am trying to use the following code (as suggested) for a Silverlight 4 C# project to change an image (But it does not know "BitmapImage":

Image myImage = new Image();
myImage.Source = new BitmapImage(new Uri("myPicture.jpg", UriKind.RelativeOrAbsolute));
LayoutRoot.Children.Add(myImage);

In order to use the BitmapImage class you have to include the Imaging Library at the top of your code...

using System.Windows.Media.Imaging;
Problems using BitmapImage constructor
In order to utilize the BitmapImage class you have to include the Imaging library.

using System.Windows.Media.Imaging;