MatrixTransform旋转然后在xaml中翻译图像

时间:2016-01-07 09:52:05

标签: wpf vb.net xaml rotation matrix-transform

我正在尝试旋转然后使用简单的Matrix.translate和Matrix.rotate方法转换图像,但出于某种原因,当它旋转90º或270º时,如果我尝试将其平移甚至一英寸它会旋转失控并远离中心。如果它是180度,它会向相反方向射出。

图像的xaml是这样的:

<Border x:Name="Border" Grid.Row="0" MinHeight="100" MinWidth="100" VerticalAlignment="Center" HorizontalAlignment="Center" ClipToBounds="True"  BorderBrush="Black" BorderThickness="1" >
     <Image x:Name="imgImagem" MaxHeight="550" MaxWidth="950"
          Margin="10,10,10,10"  MouseWheel="imgImagem_MouseWheel"
          MouseLeftButtonDown="imgImagem_MouseLeftButtonDown" 
      />    
</Border>

轮换:

Private Sub imgImagem_MouseRightButtonDown(sender As Object, e As MouseButtonEventArgs) Handles imgImagem.MouseRightButtonDown
        Dim m As Matrix = imgImagem.RenderTransform.Value
        m.RotateAt(90, imgImagem.ActualWidth / 2, imgImagem.ActualHeight / 2)
        imgImagem.RenderTransform = New MatrixTransform(m)
End Sub

翻译:

    Private Sub imgImagem_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs)

        If imgImagem.IsMouseCaptured Then
            Return
        End If
        imgImagem.CaptureMouse()
        start = e.GetPosition(imgImagem)
        origin.X = imgImagem.RenderTransform.Value.OffsetX
        origin.Y = imgImagem.RenderTransform.Value.OffsetY

    End Sub

    Private Sub imgImagem_MouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs) Handles imgImagem.MouseLeftButtonUp
        imgImagem.ReleaseMouseCapture()
    End Sub

    Private Sub imgImagem_MouseMove(sender As Object, e As Input.MouseEventArgs) Handles imgImagem.MouseMove
        Dim m As Matrix = imgImagem.RenderTransform.Value

        If Not imgImagem.IsMouseCaptured Then
            Return
        End If
        Dim p As Point = e.MouseDevice.GetPosition(imgImagem)
        m.Translate((p.X - start.X), (p.Y - start.Y))

        imgImagem.RenderTransform = New MatrixTransform(m)
    End Sub

任何关于它是什么使它旋转出来的线索,或者我做错了什么来呈现这种行为?

1 个答案:

答案 0 :(得分:0)

只需根据角度区分翻译:

        x = p.X - start.X
        y = p.Y - start.Y

        Select Case angulo
            Case 0
                m.Translate(x, y)
            Case 90
                m.Translate(-y, x)
            Case 180
                m.Translate(-x, -y)
            Case 270
                m.Translate(y, -x)
        End Select