Display Image in Silverlight


Viewing Images

The Image object is used to view images in XAML and Silverlight. The Source property of the image is used to specify the location of the image. This property can be an absolute URL or a relative URL. The following code snippet views a jpg file using the absolute path.

<Image Source="Tree.jpg" />

The following code snippet uses an absolute URL to view a file from a website.

<Image Source="http://www.c-sharpcorner.com/UploadFile/AuthorImage/mahesh.jpg" />

Stretching Images

The Stretch property of the Image class that is of type Stretch enumeration is used to stretch an image. It has values None, Uniform, UniformToFill, and Fill. The Stretch enumeration is defined in Table 2. The Width and the Height property of the Image class represent the width and the height of an image.

The Width and Height properties represent the width and height of the image control.

The following code snippet views an image and stretches to uniform with width and height of the image to 200 and 200 respectively.

<Image Source="Dock.jpg" Stretch="Uniform"

       Width="200" Height="200" Name="Media" />  


Loading Images at Run-time

The Source property can be set at run-time by creating a BitmapImage. The actual image file is passed within a new Uri.

The following code snippet create a BitmapImage from a file and sets the Source property of an Image control.


Media.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("1.jpg", UriKind.RelativeOrAbsolute));

Summary

 

In this article, I discussed how we can use an Image control and view images in Silverlight and C#. 

Up Next
    Ebook Download
    View all
    Learn
    View all