How To View An Image Using View Box Control In Universal Application Development With XAML And C#

Before reading this article, please go through the article's link, given below-

  1. Introduction To Universal Windows Platform (UWP) App Development Using Windows 10 And Visual Studio 2015

By reading this article, you can learn, how to use view an image, using View Box Control to open an image file in Universal Windows Apps development with XAML and Visual C#.

The following important tools are required for developing UWP-

  1. Windows 10 (Recommended)
  2. Visual Studio 2015 Community Edition (It is a free software available online)

Now, we can discuss step by step App development.

Step 1- Open Visual Studio 2015 -> Start -> New Project-> Select Universal (under Visual C#->Windows)-> Blank App -> Give the Suitable Name for your App (UWPViewApp) ->OK.

Blank App

Step 2- Choose the Target and minimum platform version your Windows Universal Application will support. Afterwards, the project creates App.xaml and MainPage.xaml.

version

Step 3- Open (double click) the file MainPage.xaml in Solution Explorer and click the Toolbox tab on the left to open the list of common XAML controls. Expand common XAML controls and drag the required control to the middle of the design canvas

Add TextBlock control, change the name and text property,

Add TextBlock control

Add TextBlock control, change the name and text property,

Add TextBlock control

Add Button control and change the name and content property,

Add Button control

Afterwards, add a ViewBox Control and change the name property.

 add an ViewBox Control

Add an image control inside the View Box.

Add an image control

Double click the button control. It generates click event and add the code, given below-

  1. private async void btnBrowse_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     //Using File Picker top open your Media File  
  4.     FileOpenPicker openPicker = new FileOpenPicker();  
  5.     openPicker.ViewMode = PickerViewMode.Thumbnail;  
  6.     openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;  
  7.     openPicker.FileTypeFilter.Add(".jpg");  
  8.     openPicker.FileTypeFilter.Add(".png");  
  9.     Windows.Storage.StorageFile file = await openPicker.PickSingleFileAsync();  
  10.     if (file != null) {  
  11.         var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);  
  12.         var image = new BitmapImage();  
  13.         image.SetSource(stream);  
  14.         imgDisp.Source = image;  
  15.     }  
  16. }  
code

Note: Automatically, the code, given below, will be generated in XAML code view, while we are done in the design view-
  1. <Page x:Class="UWPViewBox.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPViewBox" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">  
  2.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  3.         <TextBlock x:Name="tblTitle" HorizontalAlignment="Left" Margin="120,53,0,0" TextWrapping="Wrap" Text="View Box Test" VerticalAlignment="Top" FontWeight="Bold" />  
  4.         <TextBlock x:Name="tblFileSelect" HorizontalAlignment="Left" Margin="78,116,0,0" TextWrapping="Wrap" Text="Select Your Image" VerticalAlignment="Top" />  
  5.         <Button x:Name="btnBrowse" Content="Browse" HorizontalAlignment="Left" Margin="217,110,0,0" VerticalAlignment="Top" Click="btnBrowse_Click" />  
  6.         <Viewbox x:Name="VBImage" HorizontalAlignment="Left" Height="348" Margin="28,191,0,0" VerticalAlignment="Top" Width="289">  
  7.             <Image x:Name="imgDisp" Height="100" Width="100" /> </Viewbox>  
  8.     </Grid>  
  9. </Page>  
Step 4 - Deploy your App in Local Machine.

Select an image, using File open picker.

File open picker

Image displayed in the View Box Control is given below-

View Box Control

Summary

Now, you have successfully created and tested view an image, using View Box Control in Visual C# - UWP environment. 


Similar Articles