XNA GIF Animation Library problem
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++)
{
SurfaceForma开发者_如何学编程t 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
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.
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);
}
}
}
精彩评论