Freigeben über


Image.Source Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the source for the image.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Property Source As ImageSource
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.

Remarks

Dependency property identifier field: SourceProperty

NoteNote:

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. https://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.

Examples

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);
Dim myImage As Image = 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. https://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.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

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

Platforms

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