title: Reading Pixels from an image byte array
body: <p>I have a jpeg image. I save this bitmapdata to a byte array.</p>
  
  <p>This jpeg has a width of 100 and a height of 100.</p>
  
  <p>I want to extract an image of Rectanlge(10,10,20,20);</p>
  
  <p>Obviously, I can interact through this byte array but I am unsure how to relate the x,y pixels of what I want to this byte array.  I know that I have to use the stride and pixel size which is 4 as it is rgba.</p>
  
  <p>I have this which was from this link <a href="https://stackoverflow.com/questions/9688454/cropping-an-area-from-bitmapdata-with-c-sharp#answer-9691388">cropping an area from BitmapData with C#</a>.</p>
  
  <pre><code>Bitmap bmp = new Bitmap(_file);
  Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
   BitmapData rawOriginal = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
  
  int origByteCount = rawOriginal.Stride * rawOriginal.Height;
  byte[] origBytes = new Byte[origByteCount];
  System.Runtime.InteropServices.Marshal.Copy(rawOriginal.Scan0, origBytes, 0, origByteCount);
  
  int BPP = 4;  
  int width = 20;
  int height = 20;
  int startX = 10;
  int startY = 10;
  
  for (int i = 0; i &lt; height; i++)
  {
  for (int j = 0; j &lt; width * BPP; j += BPP)
  {
  int origIndex = (startX * rawOriginal.Stride) + (i * rawOriginal.Stride) + (startY * BPP) + (j);
  int croppedIndex = (i * width * BPP) + (j);
                          //copy data: once for each channel
  for (int k = 0; k &lt; BPP; k++)
  {
  croppedBytes[croppedIndex + k] = origBytes[origIndex + k];
  }
  }
  }
  </code></pre>
  
  <p>But this:</p>
  
  <pre><code>int origIndex = (startX * rawOriginal.Stride) + (i * rawOriginal.Stride) + (startY * BPP) + (j);
  </code></pre>
  
  <p>I found is incorrect.</p>
  
  <p>Does anyone know what value I should set here please?</p>
  
  <p>thanks</p>