Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
Implementing Animation

The RasterPictureBox namespace provides a simple "PictureBox" Windows.Forms control with which to display images. Unlike the RasterImageViewer, it does not support scrolling or zooming. However, it does support animation with the RasterImageAnimator.

Using a RasterPictureBox object is similar to using a RasterImage object. Follow the same steps as those listed in the following section, except instead of using a RasterImage constructor use a RasterPictureBox constructor.

Implementing Animation

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.

Animated GIF Files

Before you load an animated GIF file, you can use the RasterCodecs.GetInformation method to get the file's global animation information, which is updated in the CodecsImageInfo.Gif property. You can use these values in the animation playback

When you load an animated GIF file, using RasterCodecs.Load, the following animation fields in each RasterImage are updated with information from the file: RasterImage.AnimationOffset, RasterImage.AnimationDelay, RasterImage.AnimationBackground, RasterImage.Transparent, RasterImage.TransparentColor and RasterImage.AnimationDisposalMethod. These properties could be unique for each frame in the image. Also, the global animation information are loaded into the following properties: RasterImage.AnimationGlobalLoop, RasterImage.AnimationGlobalSize and RasterImage.AnimationGlobalBackground. These properties are used in the animation playback

When you save an animated GIF file, using RasterCodecs.Save, these same RasterImage properties are saved in the file. In addition, the global animation information will be used from the RasterImage automatically. Only the global palette needs to be set manually using the CodecsGifSaveOptions.SetAnimationPalette method and the CodecsGifSaveOptions.UseAnimationPalette property.

Code Examples

Example LocationDescription
RasterImageAnimator ExampleAn example to show how to will load an animated GIF file and paints on the surface of a given panel control
RasterImage.AnimationGlobalLoop ExampleAn example to show how to create an animated GIF file
RasterImage.GetTrueColorValue ExampleAn example to show how to create transparent GIF file

See also Introduction Animation