public class RasterImageAnimator : IDisposable Public Class RasterImageAnimatorImplements System.IDisposable
public ref class RasterImageAnimator : public System.IDisposable 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:
In a loop that processes each frame in the multi-frame image, do the following:
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.
This example will load an animated GIF file and paint on the surface of the given Panel.
using Leadtools;using Leadtools.Codecs;using Leadtools.ImageProcessing;using Leadtools.Drawing;[Ignore()]public void RasterImageAnimatorExample(Panel panel){// Initialize the RasterCodecs objectRasterCodecs codecs = new RasterCodecs();// When loading the animated GIF file, we have two means to load all pages into memory// Option 1: Set the Load All Pages to true, then specify just the filenamestring fileName = Path.Combine(LEAD_VARS.ImagesDir, "Eye.gif");codecs.Options.Load.AllPages = true;RasterImage animatedImage = codecs.Load(filename);// Option 2: Specify which pages to load, -1 means all pages// Load the animated GIF file//RasterImage animatedImage = codecs.Load(filename, 0, CodecsLoadByteOrder.Bgr, 1, -1)// Create the target image, we want it to be in the animated image sizeRasterImage 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 imageanimatedImage.CopyPaletteTo(targetImage);// Create the RasterImageAnimator objectRasterImageAnimator animator = new RasterImageAnimator(targetImage, animatedImage);// Animate it// Use GDI+ paint engine to support transparent colorsRasterPaintProperties props = RasterPaintProperties.Default;props.PaintEngine = RasterPaintEngine.GdiPlus;Graphics g = panel.CreateGraphics();RasterImageAnimatorState state;do{LeadRect srcRect = new LeadRect(0, 0, targetImage.ImageWidth, targetImage.ImageHeight);LeadRect updateRect;LeadRect destRect;state = animator.Process();switch (state){case RasterImageAnimatorState.WaitDelay:case RasterImageAnimatorState.WaitInputDelay:case RasterImageAnimatorState.Render:// Continue processingbreak;case RasterImageAnimatorState.WaitInput:// In case the animated image has the "wait for user input" flags,// cancel the waitinganimator.CancelWait();break;case RasterImageAnimatorState.PostClear:case RasterImageAnimatorState.PostRender:// Get the area in the target image that has changedupdateRect = animator.GetUpdateRectangle(true);// Paint itdestRect = new LeadRect(0, 0, targetImage.ImageWidth, targetImage.ImageHeight);RasterImagePainter.Paint(targetImage, g, srcRect, updateRect, destRect, destRect, props);break;default:break;}}while (state != RasterImageAnimatorState.End);g.Dispose();animator.Dispose();targetImage.Dispose();animatedImage.Dispose();codecs.Dispose();}static class LEAD_VARS{public const string ImagesDir = @"C:\LEADTOOLS21\Resources\Images";}
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.ImageProcessingImports Leadtools.DrawingSub RasterImageAnimatorExample(ByVal panel As Panel)' Initialize the RasterCodecs objectDim codecs As New RasterCodecs()' Load the animated GIF fileDim fileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Eye.gif")Dim animatedImage As RasterImage = codecs.Load(fileName)' Create the target image, we want it to be in the animated image sizeDim 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 imageanimatedImage.CopyPaletteTo(targetImage)' Create the RasterImageAnimator objectDim animator As New RasterImageAnimator(targetImage, animatedImage)' Animate it' Use GDI+ paint engine to support transparent colorsDim props As RasterPaintProperties = RasterPaintProperties.Defaultprops.PaintEngine = RasterPaintEngine.GdiPlusDim g As Graphics = panel.CreateGraphics()Dim state As RasterImageAnimatorStateDoDim srcRect As New LeadRect(0, 0, targetImage.ImageWidth, targetImage.ImageHeight)Dim updateRect As LeadRectDim destRect As LeadRectstate = animator.Process()Select Case (state)Case RasterImageAnimatorState.WaitDelay,RasterImageAnimatorState.WaitInputDelay,RasterImageAnimatorState.Render' Continue processingCase RasterImageAnimatorState.WaitInput' In case the animated image has the "wait for user input" flags,' cancel the waitinganimator.CancelWait()Case RasterImageAnimatorState.PostClear,RasterImageAnimatorState.PostRender' Get the area in the target image that has changedupdateRect = animator.GetUpdateRectangle(True)' Paint itdestRect = New LeadRect(0, 0, targetImage.ImageWidth, targetImage.ImageHeight)RasterImagePainter.Paint(targetImage, g, srcRect, updateRect, destRect, destRect, props)End SelectLoop While (state <> RasterImageAnimatorState.End)g.Dispose()animator.Dispose()targetImage.Dispose()animatedImage.Dispose()codecs.Dispose()End SubPublic NotInheritable Class LEAD_VARSPublic Const ImagesDir As String = "C:\LEADTOOLS21\Resources\Images"End Class
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
