3

我的表单上的 PictureBox 中有一个动画 gif,但它总是循环播放,我怎样才能让它只播放一次?

在另一个程序(例如浏览器)中查看 gif 时,它只播放一次,这是应该的。然而,在我的表单中,它一直在循环,动画循环之间只有很短的停顿。

4

3 回答 3

5

这花了我一段时间......在这里我获得了帧数并为 gif 设置动画直到帧结束。

Public Class Form1

    Dim animatedImage As New Bitmap("a.gif")
    Dim currentlyAnimating As Boolean = False
    Dim framecounter As Integer = 0
    Dim framecount As Integer
    Dim framdim As Imaging.FrameDimension
    Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)
        PictureBox1.Invalidate()
    End Sub

    Sub AnimateImage()
        If Not currentlyAnimating Then
            ImageAnimator.Animate(animatedImage, New EventHandler(AddressOf Me.OnFrameChanged))
            currentlyAnimating = True
        End If
    End Sub

    Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        If framecounter < framecount Then
            AnimateImage()
            ImageAnimator.UpdateFrames()
            e.Graphics.DrawImage(Me.animatedImage, New Point(0, 0))
            framecounter += 1
        End If
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        framdim = New Imaging.FrameDimension(animatedImage.FrameDimensionsList(0))
        framecount = animatedImage.GetFrameCount(framdim)
    End Sub

End Class
于 2013-01-03T22:12:54.113 回答
1

这是一个相当简单的解决方案。我为动画 gif 的一个循环的总组合时间创建了一个计时器。当计时器停止时,它会将图像更改为图片框中的静止图片。

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    PictureBox1.Image = Image.FromFile("c:\GIF\AnimatedGif.gif")
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    PictureBox1.Image = Image.FromFile("c:\GIF\StillImage.gif")
    Timer1.Enabled = False
End Sub
于 2014-12-05T00:58:01.350 回答
1

如果您可以确定周期有多长,您可以通过在适当的时间后禁用图片框来停止动画。

编辑:查看 PictureBox 源代码后,看起来没有办法在 PictureBox 本身中修改此行为。

然而,似乎有一个可行的替代方案:ImageAnimator类,它是 PictureBox 内部使用的。MSDN 文章有一个很好的示例,说明如何制作一次动画。

于 2013-01-03T21:33:44.467 回答