Universal Windows Platform – Image Rotate

Image Rotate demonstrates how to use a Storyboard to create a simple DoubleAnimation to rotate an image in the X, Y and X axis.

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 Fall Creators Update (10.0; Build 16299) 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.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Animation;

public class Library
{
    private bool _rotating = false;
    private Storyboard _rotation = new Storyboard();

    public void Rotate(string axis, ref Image target)
    {
        if (_rotating)
        {
            _rotation.Stop();
            _rotating = false;
        }
        else
        {
            DoubleAnimation animation = new DoubleAnimation
            {
                From = 0.0,
                To = 360.0,
                BeginTime = TimeSpan.FromSeconds(1),
                RepeatBehavior = RepeatBehavior.Forever
            };
            Storyboard.SetTarget(animation, target);
            Storyboard.SetTargetProperty(animation, "(UIElement.Projection).(PlaneProjection.Rotation" + axis + ")");
            _rotation.Children.Clear();
            _rotation.Children.Add(animation);
            _rotation.Begin();
            _rotating = true;
        }
    }
}

In Library.cs there are using statements to include the necessary functionality. The Storyboard is used as part of the later DoubleAnimation which will animate between 0 and 360 and this will repeat Forever after 1 second. The PlaneProjection.Rotation value for each Axis is set on the UIElement which in this case is the Image named target

Step 8

In the Solution Explorer select MainPage.xaml

vs2017-mainpage

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>
	<Grid.RowDefinitions>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="*"/>
	</Grid.RowDefinitions>
	<TextBox Grid.Row="0" Name="Value" InputScope="Url" KeyDown="Go_KeyDown" Margin="20"/>
	<Image Grid.Row="1" Margin="100" Stretch="Uniform" Name="Display">
		<Image.Projection>
			<PlaneProjection/>
		</Image.Projection>
	</Image>
</Grid>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Icon="RepeatAll" Label="Pitch" Click="Pitch_Click"/>
	<AppBarButton Icon="Rotate" Label="Roll" Click="Roll_Click"/>
	<AppBarButton Icon="Refresh" Label="Yaw" Click="Yaw_Click"/>
</CommandBar>

The first block of XAML the main user interface of the Application, this features a TextBox . The second block of XAML is is the CommandBar which contains the Pitch – to rotate the X Axis, Roll – to rotate the Y Axis and Yaw – to rotate the Z Axis

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 Go_KeyDown(object sender, KeyRoutedEventArgs e)
{
	if (e.Key == Windows.System.VirtualKey.Enter)
	{
		Display.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri(Value.Text));
	}
}

private void Pitch_Click(object sender, RoutedEventArgs e)
{
	library.Rotate("X", ref Display);
}

private void Roll_Click(object sender, RoutedEventArgs e)
{
	library.Rotate("Y", ref Display);
}

private void Yaw_Click(object sender, RoutedEventArgs e)
{
	library.Rotate("Z", ref Display);
}

Below the MainPage() Method an instance of the Library Class is created, then in the Go_KeyDown Event the Image has its Source property set to the contents of any URL entered in the TextBox, the Pitch_Click, Roll_Click and Yaw_Click Events will use the Rotate Method to indicate which Axis should be rotated and passed in the Image

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 type in the URL of any image e.g. https://dummyimage.com/300×200/000000/ffffff&text=Hello+World then press or tap enter to load this, then use the Pitch, Roll or Yaw buttons to rotate the Image

ran-image-rotate

Step 15

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

vs2017-close

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