title: Why do I get an Index out of Range exception when copying a Byte array into a Struct array?
body: <p>I have the following code for copying the content of a byte array into a struct array. However when I use it I receive an "Index out of range exception". What is wrong?</p>
  
  <pre><code>public struct pix 
  { 
      public byte b; 
      public byte g; 
      public byte r;
  };
  
  namespace ConsoleApplication4
  { 
      class Program
      {
         static  byte[] bmp = File.ReadAllBytes("D:\\x.bmp"); 
         static pix[,] img; 
         static void Main(string[] args)
         {
             int wi = 320; 
             int hi = 240; 
             int i=54; 
             img = new pix[wi-1, hi-1];
  
             Console.Write(bmp.Length-54); 
  
             for (int y = hi-1; y &gt; 0; y--)
             {
                 for (int x = wi-1; x &gt; 0; x--)
                 {
                     img[x,y].b = bmp[i]; 
                     img[x,y].g = bmp[i + 1]; 
                     img[x,y].r = bmp[i + 2];
  
                     i = i + 3; 
                 }       
            } 
  
      }
  }
  </code></pre>