<分区>
我们可以用 C# 比较两个 Image
对象吗?例如,检查它们是否相等,甚至更好地检查它们的像素有多相似?
如果可能,怎么做?
<分区>
我们可以用 C# 比较两个 Image
对象吗?例如,检查它们是否相等,甚至更好地检查它们的像素有多相似?
如果可能,怎么做?
最佳答案
您可以使用一组名为 TestApi 的工具,这是一个帮助单元测试的开源库。其中一个 API 称为 Visual Verification API ,它完全可以满足您的需求 - 它可以比较两个图像并告诉您它们是否相等:
// 1. Capture the actual pixels from a given window
Snapshot actual = Snapshot.FromRectangle(new Rectangle(0, 0, 100, 100));
// 2. Load the reference/master data from a previously saved file
Snapshot expected = Snapshot.FromFile("Expected.png"));
// 3. Compare the actual image with the master image
// This operation creates a difference image. Any regions which are identical in
// the actual and master images appear as black. Areas with significant
// differences are shown in other colors.
Snapshot difference = actual.CompareTo(expected);
// 4. Configure the snapshot verifier - It expects a black image with zero tolerances
SnapshotVerifier v = new SnapshotColorVerifier(Color.Black, new ColorDifference());
// 5. Evaluate the difference image
if (v.Verify(difference) == VerificationResult.Fail)
{
// Log failure, and save the diff file for investigation
actual.ToFile("Actual.png", ImageFormat.Png);
difference.ToFile("Difference.png", ImageFormat.Png);
}
关于c# - 如何将 Image 对象与 C# .NET 进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3384967/