如何将 GIF 动画播放到最后一帧,然后停止动画? [英] How to play a GIF animation to the last Frame, then stop the animation?
问题描述
在我的项目中,我想在 PictureBox 中播放 GIF.
我需要播放 GIF 动画包含的所有帧,然后停止动画.
我正在使用
要跟踪在 PictureBox 上绘制的当前 Frame,您需要一个 Field 来存储当前进度并将其与动画包含.
当进度到达最后一帧(或最后一帧之前的任何其他帧,无论需要什么)时,您将停止调用
导入 System.Drawing.Imaging' [...]私人动画作为图像 = My.Resources.icon_confirmation私有动画帧为整数 = 0私有 currentFrame 作为整数 = 0私有动画MaxLoops As Integer = 1私有循环为整数 = 0Private Sub btnAnimate_Click(sender As Object, e As EventArgs) 处理 btnAnimate.ClickanimationFrames = animation.GetFrameCount(New FrameDimension(animation.FrameDimensionsList(0)))动画图像()结束子公共子 AnimateImage()如果 ImageAnimator.CanAnimate(animation) 那么ImageAnimator.Animate(animation, AddressOf OnFrameChanged)万一结束子Private Sub OnFrameChanged(o As Object, e As EventArgs)如果 currentFrame >= animationFrames 那么当前帧 = 0循环 += 1If loops >= animationMaxLoops Then动画帧 = 0循环数 = 0ImageAnimator.StopAnimate(animation, AddressOf OnFrameChanged)万一别的图片框1.无效()当前帧 += 1万一结束子Private Sub pictureBox1_Paint(sender As Object, e As PaintEventArgs) 处理pictureBox1.Paint如果 animationFrames >0 那么ImageAnimator.UpdateFrames()e.Graphics.DrawImage(animation, Point.Empty)万一结束子
一个完整表单的PasteBin,使用项目资源中的图像执行动画.
In my project, I want to play a GIF in a PictureBox.
I need to play all Frames the GIF animation contains, then stop the animation.
I'm using the ImageAnimator class to animate a GIF Image, I just don't know how to stop it.
Private image As Image = My.Resources.icon_confirmation
'Private frames As Integer
Dim FDimensions As System.Drawing.Imaging.FrameDimension = New System.Drawing.Imaging.FrameDimension(image.FrameDimensionsList(0))
Dim frames As Integer = image.GetFrameCount(FDimensions)
Private Sub paintFrame(ByVal sender As Object, ByVal e As EventArgs)
If frames < 33 Then PictureBox1.Image = image Else ImageAnimator.StopAnimate(image, AddressOf StopAnim)
End Sub
Private Sub StopAnim(ByVal sender As Object, ByVal e As EventArgs)
PictureBox1.Dispose()
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If frames = 12 Then
ImageAnimator.UpdateFrames()
e.Graphics.DrawImage(image, Point.Empty)
frames -= 1
End If
End Sub
To keep track of the current Frame that's being drawn on your PictureBox, you need a Field to store the current progress and compare it with the number of Frames that the animation contains.
When the progress reaches the last Frame (or any other Frame before the last, whatever is needed), you stop the animation calling ImageAnimator.StopAnimate().
To start the animation, you first check whether ImageAnimator.CanAnimate() (it may not be able to animate the Image you specified). If it can, then you call ImageAnimator.Animate(), passing to the method the Image object and the address of the method that handles the FrameChanged
event.
This handler is used to check whether the animation should continue. If all conditions are met (not all Frames have been drawn), Invalidate() the Control used to show the animation and, in its Paint
event handler, call ImageAnimator.UpdateFrames() to change the current Frame, then e.Graphics.DrawImage() to draw the Image (drawing the Frame that is now the current).
▶ As you can see in the visual example, I'm using a Button (btnAnimate
) to start the animation. You can move that code to the Form.Shown
event handler, if you prefer.
▶ I've added a loop counter, in case the animation should loop more than once.
This is how it visually works:
Imports System.Drawing.Imaging
' [...]
Private animation As Image = My.Resources.icon_confirmation
Private animationFrames As Integer = 0
Private currentFrame As Integer = 0
Private animationMaxLoops As Integer = 1
Private loops As Integer = 0
Private Sub btnAnimate_Click(sender As Object, e As EventArgs) Handles btnAnimate.Click
animationFrames = animation.GetFrameCount(New FrameDimension(animation.FrameDimensionsList(0)))
AnimateImage()
End Sub
Public Sub AnimateImage()
If ImageAnimator.CanAnimate(animation) Then
ImageAnimator.Animate(animation, AddressOf OnFrameChanged)
End If
End Sub
Private Sub OnFrameChanged(o As Object, e As EventArgs)
If currentFrame >= animationFrames Then
currentFrame = 0
loops += 1
If loops >= animationMaxLoops Then
animationFrames = 0
loops = 0
ImageAnimator.StopAnimate(animation, AddressOf OnFrameChanged)
End If
Else
pictureBox1.Invalidate()
currentFrame += 1
End If
End Sub
Private Sub pictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles pictureBox1.Paint
If animationFrames > 0 Then
ImageAnimator.UpdateFrames()
e.Graphics.DrawImage(animation, Point.Empty)
End If
End Sub
A PasteBin of a complete Form that performs the animation using an Image from the Project's Resources.
这篇关于如何将 GIF 动画播放到最后一帧,然后停止动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!