2010-12-21 90 views
0

嘿家伙,我想使用我在网上找到的XNA Gif动画库,然后当我运行时,它保持给我一个异常说:“传入的数据的大小太大或这个资源太小了。“对于GifAnimationContentTypeReaderXNA GIF动画库问题

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

在该行 “帧[I] .SetData(J,矩形,数据,0,data.Length);” 我唐诺如何,但数据长度确实是巨大的,虽然

任何人都知道发生 THX

回答

0

字节(代码:countdata.Length)的数量怎么样了应该等于width * height * bytesPerPixel,其中bytesPerPixel取决于数据格式(默认的SurfaceFormat.Color格式是4)。

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

您还没有在我的问题中提供足够的详细信息,我可以告诉你为什么你的值不相等。

+0

这对像我这样的初学者这么复杂,我发现,使用视频会容易得多,虽然采取了更多的资源。 Thx无论如何 – 2010-12-29 03:50:58

0

你的代码有bug。 使用此代码:

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

}

+1

有关错误细节的一点细节不会伤害任何人。 – 2012-10-16 00:19:00