【问题标题】:Animated GIF won't stop looping Winforms动画 GIF 不会停止循环 Winforms
【发布时间】:2013-01-03 21:27:51
【问题描述】:

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

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

【问题讨论】:

    标签: vb.net winforms visual-studio-2010


    【解决方案1】:

    这花了我一段时间...在这里我获取帧数并为 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
    

    【讨论】:

    • 感谢你们!基本上相同的答案:)
    【解决方案2】:

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

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

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

    【讨论】:

    • 试过了,但帧之间只有 100 毫秒,很难确保它完全停在正确的位置。关闭一帧,它看起来不对。
    • @Sparrowhawk:更新了答案以包含我发现的其他信息和另一种方法。
    【解决方案3】:

    这是一个相当简单的解决方案。我为动画 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
    

    【讨论】:

      猜你喜欢
      • 2019-05-03
      • 2011-08-23
      • 1970-01-01
      • 2020-11-27
      • 2014-12-02
      • 2013-03-16
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      相关资源
      最近更新 更多