(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .

Using the Image control in Silverlight

(14 votes)
Martin Mihaylov
>
Martin Mihaylov
Joined Oct 29, 2007
Articles:   50
Comments:   70
More Articles
30 comments   /   posted on Apr 15, 2008
Tags:   image , martin-mihaylov
Categories:   Controls

This article is compatible with the latest version of Silverlight.

Introduction

In this tutorial we will provide brief introduction in the usage of the image control. It’s not very complicated, but there are a few interesting things we can find out about it.

Overview

For our examples we will use the turtle from the Sample Images in Windows. Here is the first example:

<Grid x:Name="Layout" Background="Black" Width="300" Height="200">
    <Image x:Name="MyImage" Source="/Images/GiantSeaTurtle.jpg"></Image>
</
Grid>

 

In it we use the Source property of the Image control. In this property we can set the path to the image we want to place in our application. Note: My directory “Images” is placed in the “ClientBin” directory. In case you want it to be in your project’s directory, the path should look like that: "../Images/GiantSeaTurtle.jpg".

We continue with the Stretch property of the Image control. It can be set to one of the following values: Fill, None, Uniform and UniformToFill. The default one is Uniform. We will examine every one of these properties.

Let’s begin with the Fill option:

<Image x:Name="MyImage" Source="/Images/GiantSeaTurtle.jpg"
    Stretch
="Fill"></Image>

 

It can be observed that when we use Fill the image is scaled in such a way that it takes the whole place the Grid provides for it. In this case the initial proportion of the image is not kept.

Next, we take a look at the None option:

<Image x:Name="MyImage" Source="/Images/GiantSeaTurtle.jpg"
    Stretch
="None"></Image>

 

With this property the image is not scaled. The grid is smaller than the image, so the image is cut. By default the Image control is top left aligned. Still, if we use the VerticalAlignment and HorizontalAlignment, we can change the position of the unscaled image. For example, the HorizontalAlignment can be set to Right and the VerticalAlignment to Center:

<Image x:Name="MyImage" Source="/Images/GiantSeaTurtle.jpg"
   
Stretch="None" VerticalAlignment="Center"
   
HorizontalAlignment="Right"></Image>

 

So this is the None option.

Third, we will examine the Uniform option:

<Image x:Name="MyImage" Source="/Images/GiantSeaTurtle.jpg"
    Stretch
="Uniform"></Image>

 

That’s the default value of the Stretch property. Not only does the image fit to the Grid, but also the proportion is kept. Depending on where we have black lines we can use the HorizontalAlignment or VerticalAlignment property of the control to align the Image in the Grid.

Finally, we will pay attention to the UniformToFill option:

<Image x:Name="MyImage" Source="/Images/GiantSeaTurtle.jpg"
    Stretch
="UniformToFill"></Image>

 

You can see that the image is cut on the bottom, scaled, it fills the Grid and the proportion of the image is kept. You can use the VerticalAlignment or HorizontalAlignment property (depending on the direction in which the image is cut) to align the image in the Grid.

If you use the UniformToFill option, the image fills the Grid and it looks nice and normal. On the one hand, you can lose parts of the image, if the image proportion does not match the proportions of the layout control (in our case a Grid). But on the other hand if they match then the Uniform, Fill and UniformToFill will have identical behavior. If the image has the same size as the layout control, all options of the Stretch property will behave alike.

Note: If you set the Width and Height properties of the Image control, the Stretch property will have no effect over the displaying of the image.

Image.Clip

This is another interesting thing that we can use with our Image control. With the “Clip” we can make a specific part of the image visible. It’s also possible to add an animation in it, but this will be an object of one of the upcoming articles (they will examine the Clip and the Geometry classes in details). In our case we’ll use Ellipse figure for the Clip of our image. Refer to the following:

<Image x:Name="MyImage" Source="­/Images/GiantSeaTurtle.jpg"
   
Stretch="UniformToFill">
   
<Image.Clip>
       
<EllipseGeometry x:Name="Ellipse" RadiusX="150" RadiusY="100"
           
Center="150,100"/>
   
</Image.Clip>
</Image>

 

We use the RadiusX and RadiusY properties of the EllipseGeometry to set the radiuses of the ellipse, and the Center property to position it over the image.

Conclusion

This article is just a brief tutorial that includes the key features of the Image control. It targets the developer who has just started with the Silverlight controls. Any comments are welcome.


Subscribe

Comments

  • rajahram

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by rajahram on Apr 25, 2008 22:44
    Hi Martin, I have My Image in the following folder. C:\Projects\Map\Map\ClientBin\Images My code is: But the Image is not getting visibled in the screen. Also there is another ClientBin folder in the path C:\Projects\Map\Map_Web\ClientBin\Images Please let me know in which folder do I need to place the Image. Please help me on this issue. Thanks, Rajah
  • Enrai

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by Enrai on Apr 29, 2008 00:33
    I guess you are using a Web site to host your Silverlight application. Look in your project's directory, there are two folders - one for the Silverlight application and one for the Web site. In the Web site folder there is also a ClientBin directory, which also contains a .xap file. The Web site uses this file, not the one in the Silverlight application folder, where your images are. Simply create a folder for the images in the Web site directory too.
  • rajahram

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by rajahram on May 07, 2008 00:52
    I need to display a world map. I should display the name of the country on keeping the mouse pointer over the country. Can you please help me?
  • Enrai

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by Enrai on May 09, 2008 04:14

    To do that you must know the coordinates of the region of each country on the image. This is the hardest part and it is out of this article’s scope. You need to create a handler for the MouseMove event of the image. In it you should get the position of the pointer and find to which country they correspond. Then you can use a TextBlock control to display the country name and use the Top and Left properties to position it. Here is an example handler:

    private void MyImage_MouseMove( object sender, MouseEventArgs e )

    {

    // You have to create a function that returns the name of the country based on the coordinates of the mouse, for example GetCountryName()

     

    TextBlock.Text = GetCountryName( e.GetPosition( MyImage ) );                 

               

    // Use methods of the layout control that contains the TextBlock to position it.

     

          Canvas.SetTop( TextBlock, e.GetPosition( MyImage ).Y );
          Canvas.SetLeft( TextBlock, e.GetPosition( MyImage ).X );

    }

  • rajahram

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by rajahram on May 13, 2008 03:03
    Many Thanks Enrai!!!!!! :)
  • rajahram

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by rajahram on May 13, 2008 20:49
    Hi Enrai, I have a World Map Image. On Selecting a country in the dropdown, I need to highlight the country or the particular co-ordinates by blinking it or doing any of the animations. Can you help me on this? Thanks, Rajah
  • -_-

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by Vikram on May 14, 2008 12:39
    Hi, How do I load the image if I just know the relative path of the image at runtime. for example, if all my images are in applicationpath\images folder and I get the image name as the parameter to the silverlight application, I should be able to check if the image exists and then load the image. Regards Vikram
  • Enrai

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by Enrai on May 22, 2008 10:17

    Vikram, if you try to set the source of the image to a URL in the codebehind file it won’t work. In order to set the Image control’s source dynamically you should use a BitmapImage. Here is a sample code:

    // The name of our Image control is MyImage. We will use the
    // BitmapImage class and its UriSource property to set the path to the
    // desired image. The image path can be passed as parameter.
    BitmapImage myImage = new BitmapImage();
    string imagePath = "/Images/SampleImage.jpg";
    myImage.UriSource = new Uri( imagePath, UriKind.Relative );
    MyImage.Source = myImage;

  • -_-

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by Rupak Ganguly on Jun 24, 2008 09:51

    Does Silverlight support .gif image files? I can display png and jpg files but having trouble with gifs. Any help appreciated.

  • Enrai

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by Enrai on Jul 01, 2008 05:34

    As far as I know, Silverlight doesn't support .gif files yet.

  • -_-

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by dil on Jul 04, 2008 01:14

    how can i load image dynamically in a single image control

  • -_-

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by dil on Jul 04, 2008 01:29

    how can i load different images(one after other) dynamically in a single image control with a period of time.

  • Enrai

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by Enrai on Jul 07, 2008 02:28

    Dil, yes, you can load image dynamically. Just set its source property as shown bellow:

       myImage.Source = new BitmapImage( new Uri( "GiantSeaTurtle.jpg", UriKind.Relative ) );

    You can also change the source to another image, when you set it again. To do it in an exact period of time you should use timer. In Silverlight 2 you can create timer using the DispatcherTimer class. For this example I define it in the Page() constructor:

       public Page()
       {
           InitializeComponent();
           DispatcherTimer timer = new DispatcherTimer();
           timer.Interval = new TimeSpan( 0, 0, 1 ); //the period is one second
           timer.Tick += new EventHandler( Tick );
           timer.Start();
       }
     
    The Tick event is raised every second now and in the event handler we can change the source of the image:
       private void Tick( object sender, EventArgs e )
       {
           //Change the image source here
       }

    In this event you should place your logic for changing the images. Here is a demo and if you're interested you can see the source code here.

    In Silvester Emil also uses a timer to update the data in exact periods of time, if you are interested take a look at his article too.

  • -_-

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by Baba on Aug 10, 2008 23:43

    Hi,

    Good post.

    What is solution for loading .gif images?

  • Enrai

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by Enrai on Sep 05, 2008 04:53

    For the moment Silverlight has no support for gif images. Maybe in the next versions it will be possible, but for now we can only use jpeg and png formats.

  • -_-

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by Praveen on Sep 29, 2008 04:41

    Do we have option to convert gif files to jpeg files dynamically inside our xaml.cs page?

    Thanks,

    Praveen

  • -_-

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by Rob on Jan 13, 2009 22:37

    It disgusts me the way any comments box on any technical site is used by every indian in the subcontinent to rudely ask how to perform some simple task. Research it or figure it out yourselves.

  • -_-

    RE: Using the Image control in Silverlight 2 Beta 1


    posted by Rhine on 21:14

    Thank you!

    This one is very helpful~

    Cheers

  • -_-

    RE: Using the Image control in Silverlight 2


    posted by T on Nov 03, 2009 02:31
    Why doesn't Microsoft follow the convention of Visual Studio and allow developers to use VB.  I can do C# but it takes much more thought as I am a Visual basic guy...

    -T


  • -_-

    RE: Using the Image control in Silverlight 2


    posted by Harry on Nov 23, 2009 09:29

    Hi Rob,

    Why are you diverting the post from technical to continent level.

    Regards,

    Harry

  • -_-

    RE: Using the Image control in Silverlight 2


    posted by pavan on Jan 19, 2010 15:21
    am doing image  slide show ........my task is put to play/pause buttons on images ........so the slideshow should wil start/pause...by clicking the button
  • -_-

    RE: Using the Image control in Silverlight 2


    posted by Jinesh on Mar 06, 2010 11:06
    how to save an image in an image control, where the control got the image dynamically.
  • -_-

    RE: Using the Image control in Silverlight 2


    posted by Josh on May 05, 2010 22:42
    How to zoom/magnify window on mouse over..
  • -_-

    RE: Using the Image control in Silverlight 2


    posted by Mujtaba on Jul 28, 2010 10:23
    Thanks, very useful article and easily explained!
  • HawaDera

    Re: Using the Image control in Silverlight


    posted by HawaDera on Nov 04, 2011 23:59

    hi 

    I want to make selecting tool to select rectangle,circular,and free hand bath to select apart of image and copy it do you have any idea how can i do this 

  • weitzhandler

    Re: Using the Image control in Silverlight


    posted by weitzhandler on Feb 22, 2012 17:19

    Is *.ico supported as well?

  • Enrai

    Re: Using the Image control in Silverlight


    posted by Enrai on Feb 27, 2012 15:12
    Hi, weitzhandler!

    Silverlight still supports only PNG and JPG image formats. But I found an interesting post of Joe Stegman. It is about a parser that can import ico files in Silverlight applications. Here is a link to it: http://blogs.msdn.com/b/jstegman/archive/2009/09/13/silverlight-ico-icon-decoder-parser.aspx

  • sgmanjula

    Re: Using the Image control in Silverlight


    posted by sgmanjula on Oct 11, 2012 12:26

    can we use gif images in silverlight control in silverlight 5 and how to use it

     

  • magialatina

    Re: Using the Image control in Silverlight


    posted by magialatina on Nov 19, 2013 02:52

    I have a Pictures Grid, when someone click the image I want to make the image to get a little bit bigger and load another page.  Can it be done? 

  • zeeshan121

    Re: Using the Image control in Silverlight


    posted by zeeshan121 on Jul 22, 2014 23:49

    I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job!

     buy active instagram followers

Add Comment

Login to comment:
  *      *