高级 gif 库

发布于 2024-12-17 10:40:14 字数 135 浏览 2 评论 0原文

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

I'm looking for a .NET C# gif animation library (doesn't have to be free) that will allow me to take a gif file and attach it a frame of jpeg or even another gif file. i will also need to be able to add changing delay between the frames.
The answer to similar questions here usually reference to a basic library that only allows you to add a fixed delay between static images.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2024-12-24 10:40:14

我最终修改了 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 拆分为帧并添加它们来将其添加为帧,并且它还可以添加自定义延迟。

I ended up modifying http://www.codeproject.com/KB/GDI-plus/NGif.aspx code to get what I needed and it worked! :)

for the gif source file handling I added this method:

        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;
    }

and as for the custom delays I've modified this method:

        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
    }

to sum it up - this code can add a gif as a frame by splitting it to frames and adding them, and it can also add custom delays.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文