通过gif向后移动比向前移动慢得多

Muc*_*ewe 5 c# gif visual-studio winforms

我希望能够逐帧移动gif.在下面的示例中,我使用轨迹栏来选择我想要看到的gif的哪个帧.对于设计师我只是将一个PictureBox放在屏幕的中央,并在屏幕的底部粘贴了一个TrackBar.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace TestGifProject {
    public partial class Form1 : Form {
        private Image gif;
        private FrameDimension fd;

        public Form1() {
            InitializeComponent();

            gif = Image.FromFile("PATH\\TO\\SOME\\GIF.gif");
            // just for this quick example...
            this.Width = gif.Width + 20;
            this.Height = gif.Height + 53;

            pictureBox1.Width = gif.Width;
            pictureBox1.Height = gif.Height;

            pictureBox1.Image = gif;

            fd = new FrameDimension(gif.FrameDimensionsList[0]);

            trackBar1.SetRange(0, gif.GetFrameCount(fd) - 1);
        }

        private void trackBar1_MouseUp(object sender, MouseEventArgs e) {
            gif.SelectActiveFrame(fd, trackBar1.Value);

            pictureBox1.Image = gif;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当您移动并释放轨迹栏时,框架会正确显示,但是当您向后滚动而不是向前滚动时速度要慢得多(可能快10倍),通常足够长,以使应用程序看起来像已经崩溃.有什么办法可以加快向后滚动浏览gif吗?

W0l*_*0ds 0

一种选择是创建一个列表,在其中添加每个框架。SelectActiveFrame()可能会用更复杂的逻辑来解析正确的图像,这可能是动作缓慢的原因。

在类的根目录中创建一个新列表:

private List<Image> frames = new List<Image>();
Run Code Online (Sandbox Code Playgroud)

用帧图像填充列表:

for(int i = 0; i < gif.GetFrameCount(fd); i++)
{
   gif.SelectActiveFrame(fd, i);
   frames.Add((Image)gif.Clone());
}
Run Code Online (Sandbox Code Playgroud)

然后使用列表将图像设置为PictureBox

pictureBox1.Image = frames[trackBar1.Value];
Run Code Online (Sandbox Code Playgroud)

我还没有测试过这个,但我认为它应该有效。