生成的gif具有无效的标头

时间:2018-04-09 13:33:52

标签: c# gif photoshop

我正在尝试使用 Bumpkit gif编码器生成一个gif,而gif工作(除了第一帧表现出来),当我尝试在photoshop中加载gif时,它说“可以不完整的请求,因为文件格式模块无法解析文件“。

我不知道如何检查gif的有效性,因为它在我查看时有效。这就是我使用Bumpkit库的方式:

public void SaveImagesAsGif(Stream stream, ICollection<Bitmap> images, float fps, bool loop)
        {
            if (images == null || images.ToArray().Length == 0)
            {
                throw new ArgumentException("There are no images to add to animation");
            }

            int loopCount = 0;
            if (!loop)
            {
                loopCount = 1;
            }

            using (var encoder = new BumpKit.GifEncoder(stream, null, null, loopCount))
            {
                foreach (Bitmap bitmap in images)
                {
                    encoder.AddFrame(bitmap, 0, 0, TimeSpan.FromSeconds(1 / fps));
                }
            }
            stream.Position = 0;
        }

生成gif时我做错了什么?

1 个答案:

答案 0 :(得分:1)

当您使用Bumpkit gif编码器库时,我认为您必须先调用InitHeader函数。

从GifEncoder.cs来源获取:

private void InitHeader(Stream sourceGif, int w, int h)

您可以在https://github.com/DataDink/Bumpkit/blob/master/BumpKit/BumpKit/GifEncoder.cs

查看InitHeader函数,AddFrame函数和GifEncoder.cs文件的其余部分的源代码。

所以这是对你的代码的一个小编辑:

public void SaveImagesAsGif(Stream stream, ICollection<Bitmap> images, float fps, bool loop)
    {
        if (images == null || images.ToArray().Length == 0)
        {
            throw new ArgumentException("There are no images to add to animation");
        }

        int loopCount = 0;
        if (!loop)
        {
            loopCount = 1;
        }

        using (var encoder = new BumpKit.GifEncoder(stream, null, null, loopCount))
        {
            //calling initheader function
            //TODO: Change YOURGIFWIDTHHERE and YOURGIFHEIGHTHERE to desired width and height for gif
            encoder.InitHeader(stream, YOURGIFWIDTHHERE, YOURGIFHEIGHTHERE);

            foreach (Bitmap bitmap in images)
            {
                encoder.AddFrame(bitmap, 0, 0, TimeSpan.FromSeconds(1 / fps));
            }
        }
        stream.Position = 0;
    }
相关问题