vb.net 动画 GIF 不会停止循环 Winforms

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14147553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:46:02  来源:igfitidea点击:

Animated GIF won't stop looping Winforms

vb.netwinformsvisual-studio-2010

提问by Sparrowhawk

I have an animated gif in a PictureBox on my form but it is always looping, how can I make it only play once?

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

When viewing the gif in another program (eg a browser) it only plays once, as it should. However on my form it loops all the time with only a very short jerky pause between loops of the animation.

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

回答by theGD

That took me a while... Here I obtain number of frames and animate the gif until the end of frames.

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

回答by competent_tech

If you can determine how long the cycle is, you can stop the animation by disabling the picturebox after the appropriate amount of time.

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

EDIT: After reviewing the PictureBox source code, it doesn't look like there is a way to modify this behavior in the PictureBox itself.

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

However, there does appear to be a viable alternative: the ImageAnimatorclass, which is what the PictureBox uses internally. The MSDN article has a good sample of how to animate one time.

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

回答by Rick n Truckee

Here's a fairly simple solution. I created a timer for the total combined time of one loop of the animated gif. When the timer stops it changes the image to a still picture in the picture box.

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