如何将 GIF 动画播放到最后一帧,然后停止动画?
How to play a GIF animation to the last Frame, then stop the animation?
在我的项目中,我想在 PictureBox 中播放 GIF。
我需要播放 GIF 动画包含的所有帧,然后停止动画。
我正在使用 ImageAnimator class 为 GIF 图片制作动画,我只是不知道如何停止它。
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
要跟踪在 PictureBox 上绘制的当前帧,您需要一个字段来存储当前进度并将其与动画包含的帧数进行比较。
当进度到达最后一帧(或最后一帧之前的任何其他帧,无论需要什么),您停止动画调用 ImageAnimator.StopAnimate()。
要启动动画,您首先检查是否ImageAnimator.CanAnimate() (it may not be able to animate the Image you specified). If it can, then you call ImageAnimator.Animate(),向方法传递图像对象和处理FrameChanged
事件的方法的地址。
此处理程序用于检查动画是否应继续。如果满足所有条件(不是所有的 Frames 都被绘制),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() 绘制 Image(绘制当前的 Frame)。
▶ 正如您在可视化示例中所见,我使用按钮 (btnAnimate
) 启动动画。如果愿意,您可以将该代码移至 Form.Shown
事件处理程序。
▶ 我添加了一个循环计数器,以防动画循环不止一次。
这是它的视觉效果:
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
C#版本
private Image animation = Properties.Resources.Some_GIF_Image;
private int animationFrames = 0;
private int currentFrame = 0;
private int animationMaxLoops = 1;
private int loops = 0;
private void btnAnimate_Click(object sender, EventArgs e)
{
animationFrames = animation.GetFrameCount(new FrameDimension(animation.FrameDimensionsList[0]));
AnimateImage();
}
private void AnimateImage()
{
if (ImageAnimator.CanAnimate(animation)) {
ImageAnimator.Animate(animation, OnFrameChanged);
}
}
private void OnFrameChanged(object sender, EventArgs e)
{
if (currentFrame >= animationFrames) {
currentFrame = 0;
loops += 1;
if (loops >= animationMaxLoops) {
animationFrames = 0;
loops = 0;
ImageAnimator.StopAnimate(animation, OnFrameChanged);
}
}
else {
pictureBox1.Invalidate();
currentFrame += 1;
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (animationFrames > 0) {
ImageAnimator.UpdateFrames();
e.Graphics.DrawImage(animation, Point.Empty);
}
}
A PasteBin of a complete Form 使用项目资源中的图像执行动画。
在我的项目中,我想在 PictureBox 中播放 GIF。
我需要播放 GIF 动画包含的所有帧,然后停止动画。
我正在使用 ImageAnimator class 为 GIF 图片制作动画,我只是不知道如何停止它。
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
要跟踪在 PictureBox 上绘制的当前帧,您需要一个字段来存储当前进度并将其与动画包含的帧数进行比较。
当进度到达最后一帧(或最后一帧之前的任何其他帧,无论需要什么),您停止动画调用 ImageAnimator.StopAnimate()。
要启动动画,您首先检查是否ImageAnimator.CanAnimate() (it may not be able to animate the Image you specified). If it can, then you call ImageAnimator.Animate(),向方法传递图像对象和处理FrameChanged
事件的方法的地址。
此处理程序用于检查动画是否应继续。如果满足所有条件(不是所有的 Frames 都被绘制),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() 绘制 Image(绘制当前的 Frame)。
▶ 正如您在可视化示例中所见,我使用按钮 (btnAnimate
) 启动动画。如果愿意,您可以将该代码移至 Form.Shown
事件处理程序。
▶ 我添加了一个循环计数器,以防动画循环不止一次。
这是它的视觉效果:
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
C#版本
private Image animation = Properties.Resources.Some_GIF_Image;
private int animationFrames = 0;
private int currentFrame = 0;
private int animationMaxLoops = 1;
private int loops = 0;
private void btnAnimate_Click(object sender, EventArgs e)
{
animationFrames = animation.GetFrameCount(new FrameDimension(animation.FrameDimensionsList[0]));
AnimateImage();
}
private void AnimateImage()
{
if (ImageAnimator.CanAnimate(animation)) {
ImageAnimator.Animate(animation, OnFrameChanged);
}
}
private void OnFrameChanged(object sender, EventArgs e)
{
if (currentFrame >= animationFrames) {
currentFrame = 0;
loops += 1;
if (loops >= animationMaxLoops) {
animationFrames = 0;
loops = 0;
ImageAnimator.StopAnimate(animation, OnFrameChanged);
}
}
else {
pictureBox1.Invalidate();
currentFrame += 1;
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (animationFrames > 0) {
ImageAnimator.UpdateFrames();
e.Graphics.DrawImage(animation, Point.Empty);
}
}
A PasteBin of a complete Form 使用项目资源中的图像执行动画。