我已经重写了你的答案。这应该更快更准确,并使用现有的 API。
private Color GetContrastColorBW(int x, int y, int height, int width, stream photoAsStream)
{
var rect = new Rectangle(x, y, height, width);
using Image<Rgba32> image = Image.Load<Rgba32>(photoAsStream);
// Reduce the color palette to the the dominant color without dithering.
var quantizer = new OctreeQuantizer(false, 1);
image.Mutate( // No need to clone.
img => img.Crop(rect) // Intial crop
.Quantize(quantizer) // Find the dominant color, cheaper and more accurate than resizing.
.Crop(new Rectangle(Point.Empty, new Size(1, 1))) // Crop again so the next command is faster
.BinaryThreshold(.5F, Color.Black, Color.White)); // Threshold to High-Low color. // Threshold to High-Low color, default White/Black
return image[0, 0];
}