Universal Windows Platform – JpgInfo App

JpgInfo App demonstrates how to create an application which will allow you to Open then view metadata details of a JPG picture.

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 System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.Storage.Pickers;

public class Library
{
    private async Task<Dictionary<string, string>> GetProperties(StorageFile file)
    {
        Dictionary<string, string> results = new Dictionary<string, string>();
        ImageProperties properties = await file.Properties.GetImagePropertiesAsync();
        results.Add("Name", file.Name);
        foreach (PropertyInfo property in properties.GetType().GetProperties())
        {
            results.Add(property.Name, property.GetValue(properties)?.ToString());
        }
        results.Remove("PeopleNames");
        results.Remove("Keywords");
        return results;
    }

    public async Task<Dictionary<string, string>> OpenAsync()
    {
        try
        {
            FileOpenPicker picker = new FileOpenPicker()
            {
                SuggestedStartLocation = PickerLocationId.PicturesLibrary
            };
            picker.FileTypeFilter.Add(".jpg");
            StorageFile open = await picker.PickSingleFileAsync();
            if (open != null)
            {
                return await GetProperties(open);
            }
        }
        finally
        {

        }
        return null;
    }
}

The Library Class contains a GetProperties method which returns a Dictionary – this uses the GetImagePropertiesAsync method on the Properties of a StorageFile passed into the method, it then uses Reflection to get all the properties with the GetProperties on the PropertyInfo type from the ImageProperties and this is added to the Dictionary Dictionary with the Key as a Label then the Value as the contents of that Property. The OpenAsync method uses a FileOpenPicker to open a “.jpg” file and then selected file is passed into the GetProperties method to return the Dictionary.

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:

<Grid Margin="50">
	<Grid.RowDefinitions>
		<RowDefinition Height="*"/>
		<RowDefinition Height="Auto"/>
	</Grid.RowDefinitions>
	<ListBox Grid.Row="0" Name="Display">
		<ListBox.ItemContainerStyle>
			<Style TargetType="ListBoxItem">
				<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
			</Style>
		</ListBox.ItemContainerStyle>
		<ListBox.ItemTemplate>
			<DataTemplate>
				<Grid>
					<Grid.ColumnDefinitions>
						<ColumnDefinition Width="30*"/>
						<ColumnDefinition Width="70*"/>
					</Grid.ColumnDefinitions>
					<Grid Padding="5" Grid.Column="0" Background="{ThemeResource AccentButtonBackground}">
						<TextBlock Text="{Binding Key}" Foreground="{ThemeResource AccentButtonForeground}"/>
					</Grid>
					<Grid Padding="5" Grid.Column="1" Background="{ThemeResource AccentButtonForeground}">
						<TextBlock Text="{Binding Value}" Foreground="{ThemeResource AccentButtonBackground}"/>
					</Grid>
				</Grid>
			</DataTemplate>
		</ListBox.ItemTemplate>
	</ListBox>
</Grid>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Icon="OpenFile" Label="Open" Click="Open_Click"/>
</CommandBar>

The MainPage contains the Open button to allow a JPG Picture to be opened and there’s a ListBox which is populated with image properties by setting the ItemsSource and is a list of the Key and Value from the Dictionary and with a Foreground and Background set in the DataTemplate for the ListBox.

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 async void Open_Click(object sender, RoutedEventArgs e)
{
	Display.ItemsSource = await library.OpenAsync();
}

Below the MainPage() Method an instance of the Library Class is created, then Open_Click is used to call the OpenAsync Method which will set the ListBox contents to various MetaData of the JPG 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 a jpg photo to open. Once done then you can view various properties of the image in the ListBox.

ran-lucky-roulette

You can use the following sample JPG

Big Buck Bunny

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 Simple imaging example, the purpose of this example was to show how you can extract the Image Properties for a JPG image including the Date Taken and Orientation of the Image this may also contain EXIF information used by many digital cameras, the ideal example to use is a photo taken from a Phone or Camera to see what properties are stored along with the image although there was a single sample provided which is a screenshot 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