RotateTransform in WPF using VB.NET

In this article we will learn how to use RotateTransform in WPF using VB.NET.
  • 3519
 

In this article we will learn how to use RotateTransform in WPF using VB.NET.

RotateTransform

RotateTransform is used for the process of rotating an element to a certain angle around a central point. The below example shows image rotating according to a angle.

Example

Taking a Image control and three Button control on the form. The form looks like this.

r1.gif
 

Figure 1.

XAML code

<Window x:Class="MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="MainWindow" Height="348" Width="525">

    <Grid>

        <Button Content="LeftRotate" Height="23" HorizontalAlignment="Left" Margin="386,12,0,0"Name="Button1" 
VerticalAlignment
="Top" Width="75" />

       <Image Height="150" HorizontalAlignment="Left" Margin="134,40,0,0" Name="Image1"Stretch="Fill"
 VerticalAlignment="Top" Width="200"Source="
/WpfApplication29;component/Images/flowers-image.jpg" />

        <Button Content="RightRotate" Height="23" HorizontalAlignment="Left" Margin="386,49,0,0"Name="Button2" 
VerticalAlignment
="Top" Width="75" />

        <Button Content="Same" Height="23" HorizontalAlignment="Left" Margin="386,87,0,0"Name="Button3" 
VerticalAlignment
="Top" Width="75" />

    </Grid>

</Window>

                       

Now double click on the LeftButton and add the following code.

Private Sub Button1_Click(ByVal sender As System.ObjectByVal e AsSystem.Windows.RoutedEventArgsHandles Button1.Click

        Dim rotateTransform1 As New RotateTransform()

        rotateTransform1.Angle = 30

        Image1.RenderTransform = rotateTransform1

      End Sub

Now double click on the RightButton and add the following code.

Private Sub Button2_Click(ByVal sender As System.ObjectByVal e AsSystem.Windows.RoutedEventArgsHandles Button2.Click

        Dim rotateTransform1 As New RotateTransform()

        rotateTransform1.Angle = 60

        Image1.RenderTransform = rotateTransform1

    End Sub

Now double click on the Same Button for old position.

Private Sub Button3_Click(ByVal sender As System.ObjectByVal e AsSystem.Windows.RoutedEventArgsHandles Button3.Click

        Dim rotateTransform1 As New RotateTransform()

        Image1.RenderTransform = rotateTransform1

    End Sub

Now run the application and test it.

r2.gif
 

Figure 2.

Now click on the LeftButton. The image looks like this.

r3.gif

Figure 3.

Now click on the same Button. The image looks like this.

r2.gif

Figure 4.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.