BitmapSource has a CopyPixels method that can be used to get one or more pixel values.
A helper method that receives a single pixel value at a given pixel coordinate may look like the one shown below. Please note that it may need to be expanded to support all necessary pixel formats.
public static Color GetPixelColor(BitmapSource bitmap, int x, int y) { Color color; var bytesPerPixel = (bitmap.Format.BitsPerPixel + 7) / 8; var bytes = new byte[bytesPerPixel]; var rect = new Int32Rect(x, y, 1, 1); bitmap.CopyPixels(rect, bytes, bytesPerPixel, 0); if (bitmap.Format == PixelFormats.Bgra32) { color = Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]); } else if (bitmap.Format == PixelFormats.Bgr32) { color = Color.FromRgb(bytes[2], bytes[1], bytes[0]); }
You would use a method like this:
var topLeftColor = GetPixelColor(bitmap, 0, 0); var topRightColor = GetPixelColor(bitmap, bitmap.PixelWidth - 1, 0); var bottomLeftColor = GetPixelColor(bitmap, 0, bitmap.PixelHeight - 1); var bottomRightColor = GetPixelColor(bitmap, bitmap.PixelWidth - 1, bitmap.PixelHeight - 1);
source share