Universal Windows Platform – SvgView App

SvgView App shows how to create an application which will allow you to Open and Display Svg Files

Step 1

If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.

vs2017

Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 3

From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
vs2017-new-project-window

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10, version 1803 (10.0; Build 17134) which is the April 2018 Update and the Minimum Version to be the same.

vs2017-target-platform

The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates

Step 5

Once done select from the Menu, Project, then Add New Item…

vs2017-project-add-new-item

Step 6

From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project

vs2017-add-new-item-library

Step 7

Once in the Code View for Library.cs the following should be entered:

using System;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;

public class Library
{
    public async void Open(Image display)
    {
        try
        {
            FileOpenPicker picker = new FileOpenPicker()
            {
                SuggestedStartLocation = PickerLocationId.PicturesLibrary
            };
            picker.FileTypeFilter.Add(".svg");
            StorageFile open = await picker.PickSingleFileAsync();
            if (open != null)
            {
                SvgImageSource source = new SvgImageSource()
                {
                    RasterizePixelHeight = display.ActualHeight,
                    RasterizePixelWidth = display.ActualWidth
                };
                if(await source.SetSourceAsync(await open.OpenReadAsync())
                    == SvgImageSourceLoadStatus.Success)
                {              
                    display.Source = source;
                }               
            }
        }
        finally
        {
            // Ignore Exceptions
        }
    }
}

In the Code File for Library there are using statements to include the necessary functionality and in the Library Class has an Open Method which has an Image Parameter passed in, within a try and catch Block to help handle any Exception there is a FileOpenPicker which is used to select a png File it then uses SvgImageSource and SetSourceAsync to a file selected with the FileOpenPicker with OpenReadAsync and sets the Source of the passed in Image Control, the RasterizePixelHeight and RasterizePixelWidth Methods are used to set the size of the resulting Image.

Step 8

In the Solution Explorer select MainPage.xaml

vs2017-mainpage-library

Step 9

From the Menu choose View and then Designer

vs2017-view-designer

Step 10

The Design View will be displayed along with the XAML View and in in this between the Grid and /Grid elements, enter the following XAML:

<Image Margin="50" Name="Display" Width="400" Height="400" 
VerticalAlignment="Center" HorizontalAlignment="Center"/>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Icon="OpenFile" Label="Open" Click="Open_Click"/>
</CommandBar>

Within the main Grid Element, the first block of XAML is an Image Control. The second block of XAML is a CommandBar with AppBarButton for Open which calls Open_Click.

Step 11

From the Menu choose View and then Code

vs2017-view-code

Step 12

Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:

Library library = new Library();

private void Open_Click(object sender, RoutedEventArgs e)
{
	library.Open(Display);
}

There is an Open_Click Event Handler which will will call the Open Method of the Library Class.

Step 13

That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-local-machine

Step 14

Once the Application has started running you can tap on the Open Button then you need to select a svg file which will then be displayed within the Image Control

uwp-ran-svgview-app

You can use the following sample Scalable Vector Graphics File

Download

Step 15

To Exit the Application select the Close button in the top right of the Application

vs2017-close

SvgView App is a simple application that shows how easy it is to display a svg Image in your application and scale the Image appropriately.

Creative Commons License

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s