0

I am trying to make a video animation from WPF controls. So far i draw a line to a Canvas, change the position by 1 pixel, save the canvas as an image and do the same again until the animation is completed. Now there is the Storyboard functionality which lets me create an animation like this:

var fade = new DoubleAnimation()
            {
                From = 0,
                To = 1,
                Duration = TimeSpan.FromSeconds(5),
            };

            Storyboard.SetTarget(fade, line);
            Storyboard.SetTargetProperty(fade, new PropertyPath(Line.OpacityProperty));

            var sb = new Storyboard();
            sb.Children.Add(fade);
            sb.Begin();

That simply applies a fade-in animation on a line.

The Question is: can i stop the animation every step, do something and then continue? i did not find an appropiate event to do that. What i tried was to override the canvas, like this:

class VideoableCanvas: Canvas
    {

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            Debug.WriteLine("Canvas OnRender");
        }
        protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
        {
            base.OnVisualChildrenChanged(visualAdded, visualRemoved);
            Debug.WriteLine("Canvas OnVisualChildrenChanged");
        }       
    }

And used like this:

<local:VideoableCanvas x:Name="canvas">

        <Line X1="10" X2="100" Y1="20" Y2="100" StrokeThickness="4" Stroke="Black" Name="line" />
    </local:VideoableCanvas>

But those Events to only fire once. The Storyboard itself also does not seem to have any event halting the animation every frame.

How do i stop an animation every frame, or possibly have a "OnReDraw" Event on a canvas or a Window?

Jens Caasen
  • 597
  • 1
  • 6
  • 19
  • Take a look at [Custom Animations Overview](https://learn.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/custom-animations-overview). You could perhaps intercept the `AnimationTimeline.GetCurrentValue` method. It may also help to take a look at [Per-Frame Animation: Bypass the Animation and Timing System](https://learn.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/property-animation-techniques-overview#per-frame-animation-bypass-the-animation-and-timing-system). – Clemens Aug 22 '20 at 11:24
  • Another approach is to keep your capturing outside of the render logic. https://stackoverflow.com/questions/4068414/how-to-capture-screen-to-be-video-using-c-sharp-net. As Clemens has alluded to, trying to block the animation may just cause it to render the first and last frames if you do not take control of the timing system. – Jason Aug 22 '20 at 11:29

0 Answers0