MatrixTransform 在 xaml 中旋转然后转换图像
MatrixTransform rotate then translate image in xaml
我正在尝试使用简单的 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
是否有任何线索说明是什么导致它出现这种情况,或者我做错了什么导致它出现这种行为?
只需要像这样根据角度来区分翻译:
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
我正在尝试使用简单的 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
是否有任何线索说明是什么导致它出现这种情况,或者我做错了什么导致它出现这种行为?
只需要像这样根据角度来区分翻译:
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