Partager via


Source (Image)

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

Gets or sets the source for an image by specifying the Uniform Resource Identifier (URI) for its image file location.

<Image Source="string"  .../>
value = object.Source
object.Source = value

Property Value

Type: string, or BitmapImage

The URI for the image displayed by the Image, or potentially a BitmapImage object in the get usage.

This property is read/write. The default value is null.

Managed Equivalent

Source

Remarks

The Source property can be used to reference images in the JPEG and PNG file formats.

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

Relative references are permitted. For the JavaScript API and using a source that references a XAML file, the starting location for the relative reference is the HTML page that contains the current Silverlight plug-in.

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

The JPEG and PNG file name extensions are not required to be in the URI. However, if the retrieved file is not determined to be either in JPEG or PNG format, a run-time exception is thrown.

If you are not accessing cross-domain sources, you should consider using the SetSource method instead of setting Source directly. This method enables you to track the download. Also, you can download multiple images as a package and access parts from within the package.

In JavaScript, you can set the value to null or an empty string at run time to cause the image to unload and no longer render. Doing so does not raise an ImageFailed event. In contrast, setting Source to a URI value that does not resolve will raise an ImageFailed event. Also, a SetValue error is thrown even if ImageFailed is handled.

You can specify an initial XAML value of "" (empty string) and then set the value in script at a later time, to create a nonrendering placeholder.

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

For Silverlight 2, there is also a verbose XAML usage that specifies a BitmapImage object, rather than relying on a native conversion from Uri:

<Image ...>
  <Image.Source>
    <BitmapImage UriSource="string" .../>
  </Image.Source>
</Image>

Applies To

Image

See Also

Reference