title: How to get byte[ ] from BitmapSource where byte array length is equal to BitmapSource's width x BitmapSource's height
body: <p>I am writing an application that requires the conversion of BitmapSource to byte array of pixels. Byte array's length should be equal to Bitmap's width x Bitmap's height. </p>
  
  <p>I have tried bmp.CopyPixels() function in the namespace System.Windows.Media.Imaging. But it output an array which is larger than width x height of bitmap.</p>
  
  <pre><code>private byte[] GetBytesFromBitmapSource(System.Windows.Media.Imaging.BitmapSource bmp)
          {
              int width = bmp.PixelWidth;
              int height = bmp.PixelHeight;
              int stride = width * ((bmp.Format.BitsPerPixel + 7) / 8);
              byte[] pixels = new byte[height * stride];
              bmp.CopyPixels(pixels, stride, 0);
              return pixels;
          }
  </code></pre>
  
  <p>My image (BitmapSouce) size is 2686 x 2686. I am expecting a pixel array having length 2686 x 2686 . (byte[2686 x 2686])</p>