public static unsafe void CopyPixels(this BitmapSource source, PixelColor[,] pixels, int stride, int offset)
 {
     fixed(PixelColor* buffer = &pixels[0, 0])
       source.CopyPixels(
     new Int32Rect(0, 0, source.PixelWidth, source.PixelHeight),
     (IntPtr)(buffer + offset),
     pixels.GetLength(0) * pixels.GetLength(1) * sizeof(PixelColor),
     stride);
 }
        public PixelColor[,] GetPixels(BitmapSource source)
        {
            if (source.Format != PixelFormats.Bgra32)
                source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);

            int width = source.PixelWidth;
            int height = source.PixelHeight;
            PixelColor[,] result = new PixelColor[width, height];

            BitmapSourceHelper.CopyPixels(source, result, width * 4, 0);
            //source.CopyPixels(result, width * 4, 0);
            return result;
        }
 public static void CopyPixels(this BitmapSource source, PixelColor[,] pixels, int stride, int offset)
 {
     var height = source.PixelHeight;
     var width = source.PixelWidth;
     var pixelBytes = new byte[height * width * 4];
     source.CopyPixels(pixelBytes, stride, 0);
     int y0 = offset / width;
     int x0 = offset - width * y0;
     for (int y = 0; y < height; y++)
         for (int x = 0; x < width; x++)
             pixels[x + x0, y + y0] = new PixelColor
             {
                 Blue = pixelBytes[(y * width + x) * 4 + 0],
                 Green = pixelBytes[(y * width + x) * 4 + 1],
                 Red = pixelBytes[(y * width + x) * 4 + 2],
                 Alpha = pixelBytes[(y * width + x) * 4 + 3],
             };
 }