如何在最后一帧播放GIF动画,然后停止动画? [英] How to play a GIF animation to the last Frame, then stop the animation?

查看:95
本文介绍了如何在最后一帧播放GIF动画,然后停止动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我想在PictureBox中播放GIF.
我需要播放GIF动画包含的所有帧,然后停止动画.

我正在使用

解决方案

要跟踪正在PictureBox上绘制的当前帧,您需要一个Field来存储当前进度并将其与动画包含.

当进度到达最后一个帧(或最后一个帧之前的任何其他帧,无论需要什么)时,您停止动画调用

 导入System.Drawing.Imaging'[...]私人动画,如Image = My.Resources.icon_confirmation私有animationframes作为整数= 0私有currentFrame为整数= 0私有animationMaxLoops As Integer = 1私有循环为整数= 0私有Sub btnAnimate_Click(作为对象发送,作为EventArgs发送)处理btnAnimate.ClickanimationFrames = animation.GetFrameCount(New FrameDimension(animation.FrameDimensionsList(0)))AnimateImage()结束子公共子AnimateImage()如果ImageAnimator.CanAnimate(animation)然后ImageAnimator.Animate(动画,AddressOf OnFrameChanged)万一结束子私有Sub OnFrameChanged(o作为对象,e作为EventArgs)如果currentFrame> = animationFrames然后currentFrame = 0循环+ = 1如果循环> = animationMaxLoops,则animationFrames = 0循环= 0ImageAnimator.StopAnimate(动画,AddressOf OnFrameChanged)万一别的pictureBox1.Invalidate()currentFrame + = 1万一结束子私有子pictureBox1_Paint(作为对象发送,作为PaintEventArgs发送)处理pictureBox1.Paint如果animationFrames>0然后ImageAnimator.UpdateFrames()e.Graphics.DrawImage(动画,新点(0,0))万一结束子 


一个完整表单的粘贴框,它使用项目资源中的图像执行动画.

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, New Point(0, 0))
    End If
End Sub


A PasteBin of a complete Form that performs the animation using an Image from the Project's Resources.

这篇关于如何在最后一帧播放GIF动画,然后停止动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆