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#.
Subhajit GiriPosted Aug 11, 2014, 1:50 AM
very useful article for me
Felipe RamosPosted Oct 14, 2010, 2:17 PM
I downloaded the sample and it worked fine for me. The only thing I had to do was convert the solution since I am running Silverlight 3 and it looked like the solution was targeting a lower version framework.
Mahesh ChandeditedPosted Sep 27, 2009, 12:04 PMEdited Sep 27, 2009, 12:06 PM
What is not working? Did you download the attached project? If you are using your own code, you must have Dock.jpg file in your local folder or pass the full path. This is a Silverlight project so you must have Silverlight working on your machine first. Also, this information is free. NO BODY PAYS ME FOR THIS. So you need to think somebody is posting this for free and if it does not work, may be you are doing something wrong. Think before you post any negative comments.
embarek faridPosted Sep 15, 2009, 9:30 PM
If you think you are that good than get the sample to work before posting it. You will save people lots of time/ I want to see the image to work on the asp.net develpment server. Adam