Leadtools Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
RasterImageAnimator Class
See Also  Members   Example 
Leadtools Namespace : RasterImageAnimator Class



Animates an image that has time-based frames.

Syntax

Visual Basic (Declaration)  
Public Class RasterImageAnimator 
Visual Basic (Usage) Copy Code
Dim instance As RasterImageAnimator
C#  
public class RasterImageAnimator 
C++/CLI  
public ref class RasterImageAnimator 

Example

This example will load an animated GIF file and paint on the surface of the given Panel.

Visual Basic Copy Code
Sub RasterImageAnimatorExample(ByVal panel As Panel)
   ' Initialize the RasterCodecs object
   RasterCodecs.Startup()
   Dim codecs As New RasterCodecs()
   ' Load the animated GIF file
   Dim fileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "Eye.gif"
   Dim animatedImage As RasterImage = codecs.Load(fileName)

   ' Create the target image, we want it to be in the animated image size
   Dim targetImage As New RasterImage( _
      RasterMemoryFlags.Conventional, _
      animatedImage.AnimationGlobalSize.Width, _
      animatedImage.AnimationGlobalSize.Height, _
      animatedImage.BitsPerPixel, _
      animatedImage.Order, _
      animatedImage.ViewPerspective, _
      Nothing, _
      IntPtr.Zero, _
      0)

   ' Copy the palette from the animated image to this newly created image
   animatedImage.CopyPaletteTo(targetImage)

   ' Create the RasterImageAnimator object
   Dim animator As New RasterImageAnimator(targetImage, animatedImage)

   ' Animate it

   ' Use GDI+ paint engine to support transparent colors
   Dim props As RasterPaintProperties = RasterPaintProperties.Default
   props.PaintEngine = RasterPaintEngine.GdiPlus

   Dim g As Graphics = panel.CreateGraphics()

   Dim state As RasterImageAnimatorState
   Do
      Dim srcRect As New Rectangle(0, 0, targetImage.ImageWidth, targetImage.ImageHeight)
      Dim updateRect As Rectangle
      Dim destRect As Rectangle

      state = animator.Process()

      Select Case (state)
         Case RasterImageAnimatorState.WaitDelay, _
            RasterImageAnimatorState.WaitInputDelay, _
            RasterImageAnimatorState.Render
            ' Continue processing

         Case RasterImageAnimatorState.WaitInput
            ' In case the animated image has the "wait for user input" flags,
            ' cancel the waiting
            animator.CancelWait()

         Case RasterImageAnimatorState.PostClear, _
            RasterImageAnimatorState.PostRender
            ' Get the area in the target image that has changed
            updateRect = animator.GetUpdateRectangle(True)

            ' Paint it
            destRect = New Rectangle(0, 0, targetImage.ImageWidth, targetImage.ImageHeight)

            targetImage.Paint(g, srcRect, updateRect, destRect, destRect, props)
      End Select
   Loop While (state <> RasterImageAnimatorState.End)

   g.Dispose()

   animator.Dispose()

   targetImage.Dispose()
   animatedImage.Dispose()

   codecs.Dispose()
   RasterCodecs.Shutdown()
End Sub
C# Copy Code
void RasterImageAnimatorExample(Panel panel) 

   // Initialize the RasterCodecs object 
   RasterCodecs.Startup(); 
   RasterCodecs codecs = new RasterCodecs(); 
 
   // Load the animated GIF file 
   string fileName = LeadtoolsExamples.Common.ImagesPath.Path + "Eye.gif"; 
   RasterImage animatedImage = codecs.Load(fileName); 
 
   // Create the target image, we want it to be in the animated image size 
   RasterImage targetImage = new RasterImage( 
      RasterMemoryFlags.Conventional, 
      animatedImage.AnimationGlobalSize.Width, 
      animatedImage.AnimationGlobalSize.Height, 
      animatedImage.BitsPerPixel, 
      animatedImage.Order, 
      animatedImage.ViewPerspective, 
      null, 
      IntPtr.Zero, 
      0); 
 
   // Copy the palette from the animated image to this newly created image 
   animatedImage.CopyPaletteTo(targetImage); 
 
   // Create the RasterImageAnimator object 
   RasterImageAnimator animator = new RasterImageAnimator(targetImage, animatedImage); 
 
   // Animate it 
 
   // Use GDI+ paint engine to support transparent colors 
   RasterPaintProperties props = RasterPaintProperties.Default; 
   props.PaintEngine = RasterPaintEngine.GdiPlus; 
 
   Graphics g = panel.CreateGraphics(); 
 
   RasterImageAnimatorState state; 
   do 
   { 
      Rectangle srcRect = new Rectangle(0, 0, targetImage.ImageWidth, targetImage.ImageHeight); 
      Rectangle updateRect; 
      Rectangle destRect; 
 
      state = animator.Process(); 
 
      switch(state) 
      { 
         case RasterImageAnimatorState.WaitDelay: 
         case RasterImageAnimatorState.WaitInputDelay: 
         case RasterImageAnimatorState.Render: 
            // Continue processing 
            break; 
 
         case RasterImageAnimatorState.WaitInput: 
            // In case the animated image has the "wait for user input" flags, 
            // cancel the waiting 
            animator.CancelWait(); 
            break; 
 
         case RasterImageAnimatorState.PostClear: 
         case RasterImageAnimatorState.PostRender: 
            // Get the area in the target image that has changed 
            updateRect = animator.GetUpdateRectangle(true); 
 
            // Paint it 
            destRect = new Rectangle(0, 0, targetImage.ImageWidth, targetImage.ImageHeight); 
 
            targetImage.Paint(g, srcRect, updateRect, destRect, destRect, props); 
            break; 
 
         default: 
            break; 
      } 
   } 
   while(state != RasterImageAnimatorState.End); 
 
   g.Dispose(); 
 
   animator.Dispose(); 
 
   targetImage.Dispose(); 
   animatedImage.Dispose(); 
 
   codecs.Dispose(); 
   RasterCodecs.Shutdown(); 
}

Remarks

To load and play an animated file, such as GIF, you can load the frames into a RasterImage object from the file, then play the list to a target image in a loop that displays each change in the target image. You can also create an animation sequence from scratch and save the images to create an animated GIF file.

The following is an outline of possible steps:

  1. Load a multi-frame image using Load
  2. Use the RasterImage constructor to create a target image that is the size of the images in the multi-frame image
  3. Use the RasterImageAnimator class to create an animation playback that references multi-frame image and the target image
  4. In a loop that processes each frame in the multi-frame image, do the following:
    1. Use RasterImageAnimator.Process to process the current state and get the next state of the animation
    2. Use RasterImageAnimator.GetUpdateRectangle to get the update rectangle (the portion of the target image that has changed)
    3. Use RasterImage.Paint to paint the changed portion of the image. To paint only the changed portion, use the update rectangle as the source clipping rectangle.

The animation loop can have a number of states, and you should only paint the changes in an appropriate state (for example, when the next state is RasterImageAnimatorState.PostRender. For a list of possible states, refer to RasterImageAnimatorState.

The RasterImage.AnimationDisposalMethod property in the target RasterImage determines what happens to the image in the playback loop after rendering and after any wait state, when the next state is RasterImageAnimatorState.PostDispose. The options are as follows: keep the image as it is, restore the background, or restore the previous image. (Restoring the background is a common option for animation.)

The animation loop goes continuously through the list of frames. If you want to stop at the end of the list, you can exit the loop when the next state is RasterImageAnimatorState.End.

The RasterImageAnimator class also implements the IDisposable interface, it is recommended that you follow the standard .NET dispose pattern when using the RasterImageAnimator class. For more information, refer to IDisposable.

Inheritance Hierarchy

System.Object
   Leadtools.RasterImageAnimator

Requirements

Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family

See Also