【问题标题】:XNA GIF Animation Library problemXNA GIF动画库问题
【发布时间】:2010-12-21 04:22:45
【问题描述】:

大家好,我正在尝试使用我在网上找到的 XNA​Gif 动画库,然后当我运行时,它总是给我一个异常说“传入的数据的大小对于这个资源来说太大或太小。”对于 GifAnimationContentTypeReader

        for (int i = 0; i < num; i++)
        {
            SurfaceFormat format = (SurfaceFormat) input.ReadInt32();
            int width = input.ReadInt32();
            int height = input.ReadInt32();
            int numberLevels = input.ReadInt32();
            frames[i] = new Texture2D(graphicsDevice, width, height, false, format);
            for (int j = 0; j < numberLevels; j++)
            {
                int count = input.ReadInt32();
                byte[] data = input.ReadBytes(count);
                Rectangle? rect = null;
                frames[i].SetData<byte>(j, rect, data, 0, data.Length);
            }
        }

在这一行“frames[i].SetData(j, rect, data, 0, data.Length);” 我不知道怎么做,但是数据长度确实很大

任何人都知道这是怎么发生的 谢谢

【问题讨论】:

    标签: animation xna gif


    【解决方案1】:

    字节数(在您的代码中:count 和 data.Length)应该等于 width * height * bytesPerPixel,其中 bytesPerPixel 取决于数据格式(对于默认的 SurfaceFormat.Color 格式,它是 4) .

    如果不相等,则该纹理的数据不足(或数据过多)。

    你没有在你的问题中提供足够的细节让我能够告诉你为什么你的值不相等。

    【讨论】:

    • 对于像我这样的初学者来说太复杂了,我发现使用视频会容易得多,但会占用更多资源。无论如何谢谢
    【解决方案2】:

    您的代码有错误。 使用此代码:

    namespace GifAnimation
    {
        using Microsoft.Xna.Framework.Content;
        using Microsoft.Xna.Framework.Graphics;
        using System;
        using Microsoft.Xna.Framework;
    
    public sealed class GifAnimationContentTypeReader : ContentTypeReader<GifAnimation>
    {
        protected override GifAnimation Read(ContentReader input, GifAnimation existingInstance)
        {
            int num = input.ReadInt32();
            Texture2D[] frames = new Texture2D[num];
            IGraphicsDeviceService service = (IGraphicsDeviceService)input.ContentManager.ServiceProvider.GetService(typeof(IGraphicsDeviceService));
            if (service == null)
            {
                throw new ContentLoadException();
            }
            GraphicsDevice graphicsDevice = service.GraphicsDevice;
            if (graphicsDevice == null)
            {
                throw new ContentLoadException();
            }
            for (int i = 0; i < num; ++i)
            {
                SurfaceFormat format = (SurfaceFormat)input.ReadInt32();
                int width = input.ReadInt32();
                int height = input.ReadInt32();
                int numberLevels = input.ReadInt32();
                frames[i] = new Texture2D(graphicsDevice, width, height);
                for (int j = 0; j < numberLevels; j++)
                {
                    int count = input.ReadInt32();
                    byte[] data = input.ReadBytes(count);
    
                    // Convert RGBA to BGRA
                    for (int a = 0; a < data.Length; a += 4)
                    {
                        byte tmp = data[a];
                        data[a] = data[a + 2];
                        data[a + 2] = tmp;
                    }
    
                    frames[i].SetData(data);
                }
            }
            input.Close();
            return GifAnimation.FromTextures(frames);
        }
    }
    

    }

    【讨论】:

    • 对错误细节稍加阐述不会伤害任何人。
    猜你喜欢
    • 2012-04-02
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 2014-10-22
    • 2013-04-05
    • 1970-01-01
    相关资源
    最近更新 更多