从WPF中的代码旋转图像时出现故障
本文关键字:故障 图像 旋转 WPF 代码 | 更新日期: 2023-09-27 18:19:59
我有一种情况,我想在鼠标左键按下事件上旋转WPF窗体上的控件。我试着调整我在其他地方找到的代码,但我还没有完全实现。
这是我的:
XAML:
<Image
Name="UpArrow"
Height="50"
Width="50"
RenderOptions.BitmapScalingMode="HighQuality"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Margin="10,70,0,0">
<Image.Source>
<TransformedBitmap Source="C:'Some Source File.jpg" >
<TransformedBitmap.Transform>
<RotateTransform Angle="180"/>
</TransformedBitmap.Transform>
</TransformedBitmap>
</Image.Source>
</Image>
在我的代码后面:
Private Sub UpArrow_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles UpArrow.MouseLeftButtonDown
Dim TransBitmap As TransformedBitmap = Me.UpArrow.Source
Dim Trans As RotateTransform = TransBitmap.Transform
Dim anim As New Animation.DoubleAnimation(0, New Duration(TimeSpan.FromSeconds(1)))
Trans.BeginAnimation(RotateTransform.AngleProperty, anim)
End Sub
我一直在尝试一些不同的东西,但似乎都不起作用。我能很好地阅读C#代码,所以如果你愿意回复,那就去吧,我们在这里只使用VB。我也完全可以在XAML中完成这一切,但我似乎也无法做到这一点。
谢谢!
我认为问题只是你试图设置动画的对象(转换后的位图)无法设置动画:
TransformedBitmap实现了ISupportInitialize接口,以优化多个属性的初始化。属性更改只能在对象初始化期间发生。调用BeginInit表示初始化已开始,调用EndInit表示已完成初始化。初始化后,将忽略属性更改。
如果您将RenderTransform作为图像本身的目标,则相同的代码有效:
<Image
Name="UpArrow"
Height="50"
Width="50"
MouseLeftButtonDown="UpArrow_MouseLeftButtonDown"
RenderOptions.BitmapScalingMode="HighQuality"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Margin="10,70,0,0">
<Image.RenderTransform>
<RotateTransform Angle="180"/>
</Image.RenderTransform>
<Image.Source>
<TransformedBitmap Source="Images/1.png" />
</Image.Source>
</Image>
和:
Private Sub UpArrow_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles UpArrow.MouseLeftButtonDown
Dim Trans As RotateTransform = Me.UpArrow.RenderTransform
Dim anim As New Animation.DoubleAnimation(0, New Duration(TimeSpan.FromSeconds(1)))
Trans.BeginAnimation(RotateTransform.AngleProperty, anim)
End Sub