我做错了什么?执行时出错(运行时错误)

时间:2015-05-23 11:24:58

标签: c# winforms

我做错了什么;我收到了错误消息: 在这一行:

Buffer.BlockCopy((Array) numArray, icondirentry.ImageOffset, (Array)
                 iconFile.iconImage[index], 0, icondirentry.BytesInRes);
  

错误:偏移和长度超出数组范围或计数大于从索引到源集合末尾的元素数。

此方法的完整代码为:

 public static IconClass.IconFile FromFile(string filename)
      {
        IconClass.IconFile iconFile = new IconClass.IconFile();
        byte[] numArray = File.ReadAllBytes(filename);
        GCHandle gcHandle = GCHandle.Alloc((object) numArray, GCHandleType.Pinned);
        iconFile.iconDir = (IconClass.ICONDIR) Marshal.PtrToStructure(gcHandle.AddrOfPinnedObject(), typeof (IconClass.ICONDIR));
        iconFile.iconEntry = new IconClass.ICONDIRENTRY[checked ((int) iconFile.iconDir.Count - 1 )];
        iconFile.iconImage = new byte[checked ((int) iconFile.iconDir.Count - 1)][];
        int num1 = Marshal.SizeOf((object) iconFile.iconDir);
        Type type = typeof (IconClass.ICONDIRENTRY);
        int num2 = Marshal.SizeOf(type);
        int num3 = 0;
        int num4 = checked ((int) iconFile.iconDir.Count - 1);
        int index = num3;
        while (index <= num4)
        {
          IconClass.ICONDIRENTRY icondirentry = (IconClass.ICONDIRENTRY) Marshal.PtrToStructure(new IntPtr(checked (gcHandle.AddrOfPinnedObject().ToInt64() + (long) num1)), type);
          iconFile.iconEntry[index] = icondirentry;
          iconFile.iconImage[index] = new byte[checked (icondirentry.BytesInRes - 1 )];
          Buffer.BlockCopy((Array) numArray, icondirentry.ImageOffset, (Array) iconFile.iconImage[index], 0, icondirentry.BytesInRes);
          checked { num1 += num2; }
          checked { ++index; }
        }
        gcHandle.Free();
        return iconFile;
      }

2 个答案:

答案 0 :(得分:0)

(Array) iconFile.iconImage[index]

检查索引是否不高于IconImage(数组,列表或其他任何成员)成员的大小。

答案 1 :(得分:0)

我已经解决了这个问题...... 感谢Niraj Doshi ...... 谢谢Niraj Doshi。

这是代码:

Translating English words to Pig Latin


Enter your English word:Apple
eApplay

Press ~ to exit

Enter your word:
Enter your English word:AppleCut
eCutApplay

Press ~ to exit

Enter your word:
Enter your English word:

问题是索引传递给进程。