Universal Windows Platform Controls - Part Three

In this article, you will learn about Universal Windows Platform Controls.

Before reading this article, please go through the following articles.

Introduction

Welcome again! We’re always here to bring a new topic in Universal Windows Platform App development because it’s all about Windows Phone. It’s our last part of Universal Windows Platform Controls. Today, we’ll talk about Image Control in UWP. It’s really awesome and you’ll definitely like it. So, let’s get cracking in UWP Image Control.

Working with Image Control

You can take an Image control from Toolbox or just write a simple code, as written below, along with setting four RadioButtons. We’ve talked about RadioButtons in our first part of this section. If you aren't familiar with RadioButton, feel free to take a look at it. So, our design looks like the picture below.

Image Control

Adding an image to our project

Now, we have to do a little bit of work before stepping ahead. We need to add an image to our project. Just right click in the Solution Explorer and go to Add >> New Folder. Give it a name, like “Images”.

Now, right click on the “Images” folder and go to Add >> Existing Item. Go to your destination directory to select your desired image. Select and add it.

Designing UI and Code Behind

Now, in the XAML code, show the path of the image that you’ve added in the source property of Image Control. XAML code is given below.
  1. <Grid>  
  2.     <Image x:Name="Image1" HorizontalAlignment="Left" Height="473" VerticalAlignment="Top" Width="380" Source="Images/sample.jpg" Stretch="None" Margin="10,10,0,0" />  
  3.     <RadioButton x:Name="NoneButton" Content="None" HorizontalAlignment="Left" VerticalAlignment="Top" Checked="StretchModeButton_Checked" Margin="10,488,0,0" />  
  4.     <RadioButton x:Name="FillButton" Content="Fill" HorizontalAlignment="Left" VerticalAlignment="Top" Checked="StretchModeButton_Checked" Margin="222,488,0,0" />  
  5.     <RadioButton x:Name="UniformButton" Content="Uniform" HorizontalAlignment="Left" VerticalAlignment="Top" Checked="StretchModeButton_Checked" Margin="10,555,0,0" />  
  6.     <RadioButton x:Name="UniformToFillButton" Content="UniformToFill" HorizontalAlignment="Left" VerticalAlignment="Top" Checked="StretchModeButton_Checked" Margin="222,555,0,0" />   
  7. </Grid>  
Listing 1

Here, we’ve shown the full path of our image in line seven. We will mainly show four image zooming properties- Fill, Uniform, Uniform to Fill, and Normal (None). Our four RadioButtons will handle these operations. C# code is given here.
  1. private void StretchModeButton_Checked(object sender, RoutedEventArgs e)   
  2. {  
  3.     RadioButton button = sender as RadioButton;  
  4.     if (Image1 != null) {  
  5.         switch (button.Name) {  
  6.             case "FillButton":  
  7.                 Image1.Stretch = Windows.UI.Xaml.Media.Stretch.Fill;  
  8.                 break;  
  9.             case "NoneButton":  
  10.                 Image1.Stretch = Windows.UI.Xaml.Media.Stretch.None;  
  11.                 break;  
  12.             case "UniformButton":  
  13.                 Image1.Stretch = Windows.UI.Xaml.Media.Stretch.Uniform;  
  14.                 break;  
  15.             case "UniformToFillButton":  
  16.                 Image1.Stretch = Windows.UI.Xaml.Media.Stretch.UniformToFill;  
  17.                 break;  
  18.             default:  
  19.                 break;  
  20.         }  
  21.     }  
Listing 2

Here, we’ve applied a very simple logic like Switch Case operation. We just call every RadioButton by its name, as shown in line no. 8, 11, 14, and 17, and call Windows Media Class. Image1 is our Image Control’s name. These really small lines of codes are really helpful.

Running the Application

If you run the application, it’ll look exactly like this.

Windows Phone

Windows Phone

Summary

Hope you are now able to work on Image Controls. Well, that’s it. I’ll be here with a new topic soon. Till then, good bye.
Happy coding!

Source code is here.