.NETBitmapSourceオブジェクトがあります。ビットマップの隅にある4つのピクセルを読み取り、それらすべてが白よりも暗いかどうかをテストしたいと思います。どうやってやるの?
編集:このオブジェクトをより優れたAPIを使用して別のタイプに変換してもかまいません。
.NETBitmapSourceオブジェクトがあります。ビットマップの隅にある4つのピクセルを読み取り、それらすべてが白よりも暗いかどうかをテストしたいと思います。どうやってやるの?
編集:このオブジェクトをより優れたAPIを使用して別のタイプに変換してもかまいません。
BitmapSourceには、1つ以上のピクセル値を取得するために使用できるCopyPixelsメソッドがあります。
特定のピクセル座標で単一のピクセル値を取得するヘルパーメソッドは、次のようになります。必要なすべてのピクセル形式をサポートするには、おそらく拡張する必要があることに注意してください。
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]);
}
// handle other required formats
else
{
color = Colors.Black;
}
return color;
}
次のような方法を使用します。
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);