2010-12-21 86 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