Animation XNA GIF动画库问题
嘿,伙计们,我正在尝试使用XNA 我在网上找到的Gif动画库,当我运行时,它不断给我一个异常,说“对于这个资源来说,传入的数据太大或太小。”对于GifAnimationContentTypeReaderAnimation XNA GIF动画库问题,animation,xna,gif,Animation,Xna,Gif,嘿,伙计们,我正在尝试使用XNA 我在网上找到的Gif动画库,当我运行时,它不断给我一个异常,说“对于这个资源来说,传入的数据太大或太小。”对于GifAnimationContentTypeReader for (int i = 0; i < num; i++) { SurfaceFormat format = (SurfaceFormat) input.ReadInt32(); int width = inp
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);
}
}
for(int i=0;i
在这一行“frames[i].SetData(j,rect,data,0,data.Length);”
我不知道怎么做,但数据长度确实很大
有人知道这是怎么发生的吗
thx字节数(在您的代码中:count
和data.Length
)应等于width*height*bytesperpoixel
,其中bytesperpoixel
取决于数据格式(默认SurfaceFormat.Color
格式为4)
如果它不相等,则该纹理没有足够的数据(或数据太多)
您在问题中没有提供足够的详细信息,我无法告诉您这些值不相等的原因。您的代码有错误。
使用此代码:
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);
}
}
名称空间动画
{
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.Graphics;
使用制度;
使用Microsoft.Xna.Framework;
公共密封类GifAnimationContentTypeReader:ContentTypeReader
{
受保护的覆盖GifAnimation读取(ContentReader输入,GifAnimation existingInstance)
{
int num=input.ReadInt32();
Texture2D[]帧=新的Texture2D[num];
IGraphicsDeviceService=(IGraphicsDeviceService)input.ContentManager.ServiceProvider.GetService(typeof(IGraphicsDeviceService));
if(服务==null)
{
抛出新ContentLoadException();
}
GraphicsDevice GraphicsDevice=service.GraphicsDevice;
如果(graphicsDevice==null)
{
抛出新ContentLoadException();
}
对于(int i=0;i
}对于像我这样的初学者来说,这太复杂了,我发现使用视频会更容易,尽管需要更多的资源。无论如何,对bug细节进行一点细化不会伤害任何人。