c# - 如何将 Image 对象与 C# .NET 进行比较?

标签 c# .net testing image bitmap

<分区>

我们可以用 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/

相关文章:

C# - 为高端服务器使用异步

c# - 如何实现像 SQL Server Results 面板那样的多个网格

c# - TextBox 和 .AppendText() 中的行的奇怪行为 - C#

testing - 连续属性 - 朴素贝叶斯算法中的分布

android - TouchUtils 和 ActivityInstrumentationTestCase2 用户事件单元测试用例不工作

c# - Asp.net MVC 如何用数字填充下拉列表

c# - 如何确定 .NET 类型是否为自定义结构?

c# - 如何通过传递数据库名称访问 WPF 中的 SQL Server 数据库

css - 有没有办法重新设计微软机器人的网络聊天界面?

android - 如何获取项目中单元测试的数量