Image.Source Property
Gets or sets the source for the image.
Namespace: System.Windows.Controls
Assembly: System.Windows (in System.Windows.dll)
Dependency property identifier field: SourceProperty
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 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.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
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.
- 1/30/2012
- foson
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;
- 5/23/2011
- Carbonal
- 7/19/2011
- Tom Takach
using System.Windows.Media.Imaging;
- 7/19/2011
- Tom Takach
Note: