2

I have this Canvas in my xaml definition

<Canvas Width="128" Height="128" Margin="10">
    <Canvas.CacheMode>
        <BitmapCache />
    </Canvas.CacheMode>
    <Canvas.Triggers>
        <EventTrigger RoutedEvent="Canvas.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation 
                        From="0" To="360" 
                        RepeatBehavior="Forever" 
                        Storyboard.TargetName="spin" 
                        Storyboard.TargetProperty="(RotateTransform.Angle)" 
                        />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Canvas.Triggers>
    <Canvas.RenderTransform>
        <RotateTransform x:Name="spin" Angle="0" CenterX="64" CenterY="64" />
    </Canvas.RenderTransform>
    <Image Source="Images\CircularLoading.png" Width="128" Height="128" />
</Canvas>

This animation takes 50% of my CPU, how can I improve the performance?

Naty Bizz
  • 2,262
  • 6
  • 33
  • 52
  • 1
    If all you're doing is spinning an image, perhaps an animated GIF would be better (pre-computed images always beat calculated images). [This](http://stackoverflow.com/questions/210922/how-do-i-get-an-animated-gif-to-work-in-wpf) question and answer might help. – Michael Todd May 28 '13 at 17:16

1 Answers1

3

You are spinning as fast as you can get. I guess you have two cores so at 50% one completely is busy spinning. Maybe you could add a Duration to your DoubleAnimation so it takes a few seconds spin by 360 once, reducing CPU load.

On the other hand, if by performance you mean you want to spin even faster, utilizing the other 50% CPU, too, you should look into hardware support and probably graphics frameworks. WPF is nice, but not for high-performance graphics.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • I tried add Duration in my animation but nothing changed. Also I noticed this problem appears if I put my animation in a UserControl, if I put the animation in a Window, the CPU behaves 'normal' (the app uses 3, 4%) – Naty Bizz May 28 '13 at 20:59