MattB
MattB

Reputation: 2241

Trouble Rotating Image From Code in WPF

I have a situation where I'd like to rotate a control on a WPF form on the left mouse button down event. I've tried to adapt code I've found elsewhere, but I'm just not quite getting there.

Here's what I have:

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>

And in my code behind:

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

I keep trying some different things but nothing seems to work. I can read C# code just fine, so if you're comfortable replying in that, go for it, we just use VB here. I'm also completely OK with doing this all in the XAML but I can't seem to get that to work either.

Thanks!

Upvotes: 1

Views: 273

Answers (1)

McGarnagle
McGarnagle

Reputation: 102793

I think the problem is just that the object you're trying to animate (transformed bitmap) cannot be animated:

TransformedBitmap implements the ISupportInitialize interface to optimize initialization on multiple properties. Property changes can only occur during object initialization. Call BeginInit to signal that initialization has begun and EndInit to signal that initialization has completed. After initialization, property changes are ignored.

The identical code works if you target a RenderTransform on the image itself:

<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>

And:

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

Upvotes: 1

Related Questions