Universal Windows Platform – GifView App

GifView App demonstrates how to create an application which will allow you to Open and Play an Animated Gif – plus you can also Stop the playback of the animation.

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.Media.Imaging;

public class Library
{
    public async void Open(BitmapImage display)
    {
        try
        {
            FileOpenPicker picker = new FileOpenPicker()
            {
                SuggestedStartLocation = PickerLocationId.PicturesLibrary
            };
            picker.FileTypeFilter.Add(".gif");
            StorageFile open = await picker.PickSingleFileAsync();
            if (open != null)
            {
                display.AutoPlay = false;
                await display.SetSourceAsync(await open.OpenReadAsync());
            }
        }
        finally
        {
            // Ignore Exceptions
        }
    }
}

The Library Class is quite simple with just one method called Open which contains a FileOpenPicker set to open a “.gif” file and then sets the passed in BitmapImage to be the opened file using PickSingleFileAsync to get the file and SetSourceAsync to set the image from the file with OpenReadAsync

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 this between the Grid and /Grid elements, enter the following XAML:

<Image VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="None">
	<Image.Source>
		<BitmapImage x:Name="Display"/>
	</Image.Source>
</Image>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Icon="OpenFile" Label="Open" Click="Open_Click"/>
	<AppBarButton Icon="Play" Label="Play" Click="{x:Bind Display.Play}"/>
	<AppBarButton Icon="Stop" Label="Stop" Click="{x:Bind Display.Stop}"/>
</CommandBar>

MainPage.xaml has three AppBarButton controls; Open – to trigger an event, Play which uses the Click event to trigger a bound expression to call Play on the BitmapImage and Stop to trigger a bound expression to call Stop on the BitmapImage. Plus there’s the Display itself which is the BitmapImage

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);
}

Below the MainPage() Method an instance of the Library Class is created, then Open_Click is used to call the Open Method to load an animated .gif file.

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

After the Application has started running you can then select Open then you need to select an animated gif file then once done then you can select Play to start the image animating and to end this animation select Stop

ran-gifview-app

You can use the following sample Animated Gif

animation

Step 15

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

vs2017-close

This example was inspired from the Microsoft Universal Windows Platform (UWP) app samples and in particular the Animated GIF Playback example – the goal was to create a simpler implementation of this and found that the bound expressions made it really simple anyway, so the way the animated GIF is played or stopped is the same and demonnstrated a really great technique to trigger events in another object. Also provided was a sample animated GIF which is an extract from the Big Buck Bunny video from the Blender Foundation.

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