2

I defined a Polygon like this:

            <!-- Draws a hexagon by specifying the vertices of a polygon -->
            <Polygon x:Name="polygon"
                     Points="0,50 0,120 50,170 120,170 170,120 170,50 120,0 50,0"
                     Margin="25, 0, 25, 25"
                     Stroke="Red"
                     RenderTransformOrigin="0.5,0.5">

                <Polygon.RenderTransform>
                    <CompositeTransform />
                </Polygon.RenderTransform>

                <Polygon.Fill>
                    <ImageBrush x:Name="imageBrush"
                                ImageSource="Assets/image1.jpg"
                                Stretch="Fill">
                    </ImageBrush>
                </Polygon.Fill>
            </Polygon>

I also defined a storyboard like this:

    <Storyboard x:Name="Storyboard2">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)"
                                       Storyboard.TargetName="polygon">
            <EasingDoubleKeyFrame KeyTime="0"
                                  Value="0" />
            <EasingDoubleKeyFrame KeyTime="0:0:2"
                                  Value="-360" />
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>

I start the polygon rotation like this:

        Storyboard2.BeginTime = new TimeSpan( 0 );
        Storyboard2.Begin();

The above code rotate the whole polygon as expected. However I would like to modify it, and to have the polygon stationary and just to rotate the bitmap inside the polygon.

How do I do that? Thx

eitan barazani
  • 1,123
  • 3
  • 18
  • 34
  • Have you tried to change the `TargetName` from "polygon" to "imageBrush"? – aloisdg May 12 '16 at 20:56
  • yes. I did but didn't do the trick. The application crashes. – eitan barazani May 12 '16 at 21:09
  • Yea the crash would be expected, you need to apply a [Transform](https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.brush.transform) and apply the animation to it, if I have time before I head home I'll whip up an example. – Chris W. May 12 '16 at 21:13
  • I did try this: but without success. – eitan barazani May 12 '16 at 21:15
  • Rotate is all you're trying to accomplish right? Kind of wish I had a visual of the desired result since there's often more than one way to skin a cat (as the saying goes). – Chris W. May 12 '16 at 21:15
  • Brush.Transform will only support a single Transform. CompositeTransform is for folks that generally don't understand how they work which is common and no big deal, it's just an easy way to throw whatever transforms you're working with into one thing.....you'd want just RotateTransform. – Chris W. May 12 '16 at 21:16
  • Yes. Just to rotate the image inside the polygon. – eitan barazani May 12 '16 at 21:17
  • Ah crap, it's UWP again, didn't you have one I was going to answer yesterday but I don't have a UWP environment until I get home and that won't be until later so I'd have to freehand it without testing. – Chris W. May 12 '16 at 21:18

2 Answers2

2

To rotate the image inside your polygon, you can try with ImageBrush.RelativeTransform property like following:

<Polygon x:Name="polygon"
         Margin="25, 0, 25, 25"
         Points="0,50 0,120 50,170 120,170 170,120 170,50 120,0 50,0"
         Stroke="Red">
    <Polygon.Fill>
        <ImageBrush x:Name="imageBrush" ImageSource="Assets/Capture.PNG" Stretch="Fill">
            <ImageBrush.RelativeTransform>
                <CompositeTransform CenterX="0.5" CenterY="0.5" />
            </ImageBrush.RelativeTransform>
        </ImageBrush>
    </Polygon.Fill>
</Polygon>

And change your Storyboard like following:

<Storyboard x:Name="Storyboard2">
    <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True"
                                   Storyboard.TargetName="imageBrush"
                                   Storyboard.TargetProperty="(Brush.RelativeTransform).(CompositeTransform.Rotation)">
        <EasingDoubleKeyFrame KeyTime="0" Value="0" />
        <EasingDoubleKeyFrame KeyTime="0:0:2" Value="-360" />
    </DoubleAnimationUsingKeyFrames>
</Storyboard>

Here, as the target is ImageBrush, so we need to change Storyboard.TargetProperty to (Brush.RelativeTransform).(CompositeTransform.Rotation). And as the animation targets Brush.RelativeTransform, so it's a dependent animation. We need to set EnableDependentAnimation property to true to enable this animation. For more info about dependent animation, please see Dependent and independent animations.

After this, you can call Storyboard2.Begin(); to start the animation.

Jay Zuo
  • 15,653
  • 2
  • 25
  • 49
  • This looks more correct but I don't have a way to test at the moment. I could have sworn Brush.Transform wouldn't accept CompositeTransform though. It worked for you though Jay? – Chris W. May 13 '16 at 13:54
  • Cool so it will accept Composite? Learned something new, +1 – Chris W. May 13 '16 at 19:09
1

Since I can't test on a UWP enviro at the moment, here's how it's done in WPF, same concept...but I'm thinking the syntax may be a little different, hence why I wanted to test.

Storyboard:

<Storyboard x:Key="SpinThisBugger" RepeatBehavior="Forever">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(Brush.RelativeTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="polygon">
                <EasingDoubleKeyFrame KeyTime="0:0:1" Value="360"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>

Trigger to start it:

<Window.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
        <BeginStoryboard Storyboard="{StaticResource SpinThisBugger}"/>
    </EventTrigger>
</Window.Triggers>

The Poly:

<Polygon x:Name="polygon"
                     Points="0,50 0,120 50,170 120,170 170,120 170,50 120,0 50,0"
                        HorizontalAlignment="Center" VerticalAlignment="Center"
                     Stroke="Red"
                     RenderTransformOrigin="0.5,0.5">
            <Polygon.Fill>
                <ImageBrush x:Name="imageBrush"
                                ImageSource="Images/learn-hypnosis.jpg"
                                Stretch="Fill">
                    <ImageBrush.RelativeTransform>
                        <TransformGroup>
                            <ScaleTransform CenterY="0.5" CenterX="0.5"/>
                            <SkewTransform CenterY="0.5" CenterX="0.5"/>
                            <RotateTransform CenterY="0.5" CenterX="0.5"/>
                            <TranslateTransform/>
                        </TransformGroup>
                    </ImageBrush.RelativeTransform>
                </ImageBrush>
            </Polygon.Fill>

        </Polygon>

...and the image I used, since it was nifty lol:

enter image description here

Chris W.
  • 22,835
  • 3
  • 60
  • 94
  • has only RelativeTransform or Transform. No RenderTransform – eitan barazani May 12 '16 at 21:55
  • I did try your suggestion though. It didn't crash but didn't rotate either. – eitan barazani May 12 '16 at 22:05
  • Yea Sorry @eitanbarazani I didn't have time to get back on last night, and it's Friday with the weekend coming. I'll del this in a bit until I have time to come back with an actually tested solution but hopefully Jay's answer is correct. – Chris W. May 13 '16 at 14:07
  • @eitanbarazani updated example with a working/tested example, but it's for WPF, so there may be some nuance differences in syntax. – Chris W. May 13 '16 at 14:37