XNA GIF动画库问题

发布于 2024-10-08 13:36:17 字数 901 浏览 4 评论 0原文

嘿伙计们,我正在尝试使用我在网上找到的 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);” 我不知道怎么回事,但数据长度确实很大,但

有人知道这是怎么发生的 谢谢

hey guys, i am trying to use the XNA Gif Animation Library that i found online, then when i running , it keep givving me an exception said "The size of the data passed in is too large or too small for this resource." for 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);
            }
        }

at this line "frames[i].SetData(j, rect, data, 0, data.Length);"
I donno how, but the data length is really huge though

Anyone know hows that happen
thx

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

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

发布评论

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

评论(2

阳光下的泡沫是彩色的 2024-10-15 13:36:17

字节数(在代码中:countdata.Length)应等于 width * height * bytesPerPixel,其中 >bytesPerPixel 取决于数据格式(默认的 SurfaceFormat.Color 格式为 4)。

如果不相等,则说明该纹理没有足够的数据(或数据太多)。

您在问题中没有提供足够的详细信息,因此我无法告诉您为什么您的值不相等。

The number of bytes (in your code: count and data.Length) should be equal to width * height * bytesPerPixel, where the bytesPerPixel depends on the data format (for the default SurfaceFormat.Color format it is 4).

If it is not equal, you don't have enough data (or have too much data) for the that texture.

You haven't provided enough detail in your question for me to be able to tell you why your the values are not equal.

瞎闹 2024-10-15 13:36:17

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

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

}

Your code has bug.
Use this code:

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

}

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