Я попробовал этот код, но он показывает последний кадр. и если я установлю задержку кадра на 1000, то он будет ждать одну секунду, прежде чем что-либо показывать, а затем покажет последний кадр. он не воспроизводит все кадры от первого до последнего.
Я пытался сначала загрузить анимированный gif в PictureBox, но поведение такое же.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
public class AnimatedGifControl : Control
{
private List frames;
private int currentFrameIndex;
private Timer animationTimer;
[Browsable(true)]
[Category("Behavior")]
[Description("The delay between frames in milliseconds.")]
public int FrameDelay { get; set; } = 100; // Default frame delay
public AnimatedGifControl()
{
frames = new List();
DoubleBuffered = true;
animationTimer = new Timer();
animationTimer.Tick += OnAnimationTick;
}
public void LoadGif(string gifPath)
{
frames.Clear();
using (Image gifImage = Image.FromFile(gifPath))
{
int frameCount = gifImage.GetFrameCount(FrameDimension.Time);
for (int i = 0; i < frameCount; i++)
{
gifImage.SelectActiveFrame(FrameDimension.Time, i);
frames.Add(new Bitmap(gifImage));
}
}
currentFrameIndex = 0;
if (frames.Count > 0)
{
animationTimer.Interval = FrameDelay;
animationTimer.Start();
}
}
private void OnAnimationTick(object sender, EventArgs e)
{
currentFrameIndex = (currentFrameIndex + 1) % frames.Count;
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (frames.Count > 0)
{
e.Graphics.DrawImage(frames[currentFrameIndex], 0, 0, Width, Height);
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
animationTimer.Dispose();
foreach (var frame in frames)
{
frame.Dispose();
}
}
base.Dispose(disposing);
}
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... imated-gif
Как создать элемент управления в формах Windows, который будет отображать и воспроизводить анимированный GIF-файл? ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение