public static WriteableBitmap PixelsToBitmapSource(PixelColor[,] pixels)
        {
            int width = pixels.GetLength(0);
            int height = pixels.GetLength(1);
            byte[] pixelData = new byte[width * height * 4];
            for (int y = 0; y < height; ++y)
                for (int x = 0; x < width; ++x)
                {
                    PixelColor color = pixels[x, y];
                    pixelData[(y * width + x) * 4 + 0] = color.Blue;
                    pixelData[(y * width + x) * 4 + 1] = color.Green;
                    pixelData[(y * width + x) * 4 + 2] = color.Red;
                    pixelData[(y * width + x) * 4 + 3] = color.Alpha;
                }

            WriteableBitmap bitmap = new WriteableBitmap(width, height, Dpi, Dpi, PixelFormats.Bgra32, null);
            bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixelData, bitmap.PixelWidth * 4, 0);
            return bitmap;
        }