public static PixelColor[,] GetPixelsFromBitmapSource(BitmapSource source) { if (source.Format != PixelFormats.Bgra32) source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0); int width = source.PixelWidth; int height = source.PixelHeight; byte[] pixelBytes = new byte[height * width * 4]; source.CopyPixels(pixelBytes, width * 4, 0); PixelColor[,] pixels = new PixelColor[width, height]; for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) pixels[x, y] = 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], }; return pixels; }
public static BitmapSource MaskToBitmapSource(bool[,] mask) { PixelColor[,] pixels = new PixelColor[mask.GetLength(0), mask.GetLength(1)]; PixelColor objectColor = new PixelColor(255, 0, 0, 120); PixelColor backgroundColor = new PixelColor(0, 0, 0, 0); for (int i = 0; i < mask.GetLength(0); ++i) for (int j = 0; j < mask.GetLength(1); ++j) pixels[i, j] = mask[i, j] ? objectColor : backgroundColor; return PixelsToBitmapSource(pixels); }
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; }