Skip to content

Save Gif from Gif source #8

@Dunge

Description

@Dunge

The demo use a static image, create multiple frame by rotation and save a gif. But if the original Image loaded is a Gif, it seems to create a corrupted file.

See this code:

        private static byte[] ToGifByteArray(Image image)
        {
            var frameCount = image.GetFrameCount(FrameDimension.Time);
            var times = image.GetPropertyItem(0x5100).Value;  // PropertyTagFrameDelay

            using (var memoryStream = new MemoryStream())
            {
                using (var encoder = new GifEncoder(memoryStream, null, null, -1))
                {
                    for (var idx = 0; idx < frameCount; idx++)
                    {
                        image.SelectActiveFrame(FrameDimension.Time, idx);
                        var dur = BitConverter.ToInt32(times, 4 * 0); 
                        encoder.AddFrame(image, 0, 0, TimeSpan.FromSeconds(dur));
                    }
                }

                File.WriteAllBytes(@"C:\test.gif", memoryStream.ToArray());
                return memoryStream.ToArray();
            }
        }

If I pass a Image loaded from a gif, it create a corrupted. If I comment the first two lines for the timing and the loop for the frame and pass a Image loaded from a jpeg, it create a working gif.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions