【问题标题】:advanced gif library高级 gif 库
【发布时间】:2012-01-04 21:59:22
【问题描述】:

我正在寻找一个 .NET C# gif 动画库(不一定是免费的),它可以让我获取一个 gif 文件并将其附加到 jpeg 帧甚至另一个 gif 文件。我还需要能够在帧之间添加变化的延迟。 此处类似问题的答案通常参考一个基本库,该库仅允许您在静态图像之间添加固定延迟。

【问题讨论】:

  • 它被关闭了,因为它被问到是重复的。您没有提到需要更改帧之间的延迟,或者您发现以前的答案不符合您的需求。 :)
  • 我同意,我只是没想到它会这么快关闭:)
  • 请不要在您的帖子上签名。

标签: c# .net animation gif animated-gif


【解决方案1】:

我最终修改了http://www.codeproject.com/KB/GDI-plus/NGif.aspx 代码以获得我需要的东西并且它有效! :)

对于 gif 源文件的处理我添加了这个方法:

        private bool AddGifFrames(Image image)
    {
        // implementation

        var fd = new FrameDimension(image.FrameDimensionsList[0]);
        int frameCount = image.GetFrameCount(fd);

        var frames = new List<Tuple<int, Image>>();

        if (frameCount > 1)
        {
            frames = new List<Tuple<int, Image>>();

            //0x5100 is the property id of the GIF frame's durations
            //this property does not exist when frameCount <= 1
            byte[] times = image.GetPropertyItem(0x5100).Value;

            for (int i = 0; i < frameCount; i++)
            {
                //selects GIF frame based on FrameDimension and frameIndex
                image.SelectActiveFrame(fd, i);

                //length in milliseconds of display duration
                int length = BitConverter.ToInt32(times, 4 * i);

                //save currect image frame as new bitmap
                frames.Add(new Tuple<int, Image>(length, new Bitmap(image)));
            }
        } // Not animated

        foreach (var frame in frames)
        {
            HandleFrame(frame.Item2, frame.Item1);
        }

        return true;
    }

至于自定义延迟我已经修改了这个方法:

        protected void WriteGraphicCtrlExt(int? delay)
    {
        Fs.WriteByte(0x21); // extension introducer
        Fs.WriteByte(0xf9); // GCE label
        Fs.WriteByte(4); // data block size
        int transp, disp;
        if (Transparent == Color.Empty)
        {
            transp = 0;
            disp = 0; // dispose = no action
        }
        else
        {
            transp = 1;
            disp = 2; // force clear if using transparent color
        }
        if (Dispose >= 0)
        {
            disp = Dispose & 7; // user override
        }
        disp <<= 2;

        // packed fields
        Fs.WriteByte(Convert.ToByte(0 | // 1:3 reserved
                                    disp | // 4:6 disposal
                                    0 | // 7   user input - 0 = none
                                    transp)); // 8   transparency flag

        WriteShort(delay ?? Delay); // delay x 1/100 sec
        Fs.WriteByte(Convert.ToByte(TransIndex)); // transparent color index
        Fs.WriteByte(0); // block terminator
    }

总结一下——这段代码可以通过将 gif 拆分为帧并添加它们来将其添加为帧,它还可以添加自定义延迟。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-31
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    相关资源
    最近更新 更多